Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract classs  RSS feed

 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



1.what will be execution order and why?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try running the code? What output did you get?
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Did you try running the code? What output did you get?
Show method
Dog is sleeping
Dog is eating
Show method
Cat is sleeping
Cat is eating

But question here is static show() how it is working?
and which static method will be executed first main() or show()?
But from program output is showing that static show() method is executing first?

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>But question here is static show() how it is working?
It is invoked from main. Then the sleeping and eating are invoked on a.

>>and which static method will be executed first main() or show()?
Main is your application entry point. It will always be invoked first by the JVM. In fact the JVM won't even bother with any other methods.

>>But from program output is showing that static show() method is executing first?
That is because you are invoking the show from main. Remove the show(a) statements from your main to confirm this

 
praneeth desur
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will be the output:
Show method
Dog is sleeping
Dog is eating
Show method
Cat is sleeping
Cat is eating

Execution is straght fwd thru main method, in the order of lines of code.
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I am commenting show(a) I am not getting any output.
Maneesh Godbole wrote:>>But question here is static show() how it is working?
It is invoked from main. Then the sleeping and eating are invoked on a.

>>and which static method will be executed first main() or show()?
Main is your application entry point. It will always be invoked first by the JVM. In fact the JVM won't even bother with any other methods.

>>But from program output is showing that static show() method is executing first?
That is because you are invoking the show from main. Remove the show(a) statements from your main to confirm this

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I am commenting show(a) I am not getting any output.

Yes. So it means your code is invoking the show from inside main right?
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are correct but when it is coming at
at this point it should load AnimalFactory class .and when it is coming to show(a) then it should call show() method.
Am I going correct?
praneeth desur wrote:This will be the output:
Show method
Dog is sleeping
Dog is eating
Show method
Cat is sleeping
Cat is eating

Execution is straght fwd thru main method, in the order of lines of code.
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But before show(a) I ahve given a=AnimalFactory.getAnimal(AnimalFactory.DOG); so flow should go in this way.
And when I am calling a.sleeping() in show the what will be the call.?
Maneesh Godbole wrote:
But when I am commenting show(a) I am not getting any output.

Yes. So it means your code is invoking the show from inside main right?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But before show(a) I ahve given a=AnimalFactory.getAnimal(AnimalFactory.DOG); so flow should go in this way.
And when I am calling a.sleeping() in show the what will be the call.?

The AnimalFactory just creates the Animal instance. Even when you commented out the show() from main, the instances were still created.

Think of it this way.
1) You need an animal.
This part is handled by the factory.
2) Once you have the animal you can, only then can you ask it to eat or sleep.
This part is handled by the eating() and sleeping() methods
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole when I need animal (a=AnimalFactory.getAnimal(AnimalFactory.DOG);) now AnimalFactory class will be loaded and MyDod class then Dog class so first it should print "Dog is eating " and then after "Dog is sleeping " then it will come to show(a); now it should print "show". and again in show() "a.eating and a.sleeping" what it will do. please help me
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I see where the confusion is.
You got an Animal. The animal is capable of eating and sleeping. However, it will not eat or sleep on its own. You have to explicitly tell it to eat or sleep. So until some code invokes the eat or sleep methods, the code inside these methods won't be executed.
Think of it as a 3G enabled phone. It can connect using 3G, but it will not do so until you explicitly tell it to do so.

Does this clear things?
 
Prakash Rai
Ranch Hand
Posts: 107
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your example really very good.Now am clear but again one question
in output 1st it is printing "show". but According to me it should print "dog is eating" and "Dog is sleeping" then it should print "show".
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code inside show() is executed sequentially. If you change the sequence (call show after sleeping and eating) the console will show sleeping, eating and show

but According to me it should print "dog is eating" and "Dog is sleeping" then it should print "show".

Why do you think so?
 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is really nice example.....
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!