aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what gets printed and why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what gets printed and why?" Watch "what gets printed and why?" New topic
Author

what gets printed and why?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Hi all,

Consider the following code...



Guys, I tried this program and I want to how it prints the output. Can anyone please explain me the flow?

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Anand Loni
Ranch Hand

Joined: Jan 20, 2006
Posts: 150
Hi,

Output is 22.
Flow is when you create Extension object Extension constructor gets called which will call Base constructor then i will be initialized to its default i.e 0. Base constructor calls add method with parameter as 1 which will call Extensions add method which sets i to 2. after Base constructor completion we will return to Extension contructor where add method called with value 2 so add method will set i to 6 and in bogo we called add with 8 so add of Extension is called as object passed is Extension class object so i is set to 22.


~ Anand,
SCJP 1.5
SCWCD 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Hi anand,

your quote,

Base constructor calls add method with parameter as 1 which will call Extensions add method which sets i to 2.


I'm not getting this?

Regards,
Jothi Shankar Kumar. S
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Hi Anand,

I understood now. Thanks for the explanation. So the add method in Extension class is called bcos the actual object at runtime is of type extension. So the call to add in Base constructor actually calls add in Extension where i is set to 2. Can you throw some light on this?? Probably a deeper explanation of how this call to add in subclass happens from a superclass constructor?

Regards,
Jothi Shankar Kumar. S
Harshad Khasnis
Ranch Hand

Joined: Jun 06, 2006
Posts: 48
Hi,
I modified your code and added some SOPs to track the execution of programm.

Just try running this code you can easily get what actually is happening here.



I hope now you understand the flow of execution.

Your Qoute,


Hi Anand,

I understood now. Thanks for the explanation. So the add method in Extension class is called bcos the actual object at runtime is of type extension. So the call to add in Base constructor actually calls add in Extension where i is set to 2. Can you throw some light on this?? Probably a deeper explanation of how this call to add in subclass happens from a superclass constructor?


Now regarding the call to add methode in the extention class from base class constructor.
As you can see the actual object created is of Extension type and not of Base type.
So This is the phenomenon called as runtime binding.
This is feature of Object Oriented Programming.
Means actually at the runtime it is decided which methode to call depending upon the type of object.

I hope I solved your query.
Abhijit Kolhatkar
Greenhorn

Joined: Sep 14, 2006
Posts: 10
This is from my side...
Assume you have the mentioned Inheritance Hierarchy.
The scenario is, you are creating an object of CHILD class and assigning it to a reference of BASE class(This is perfectly legal cast since base class ref can hold child class objects).
Now, though this reference belongs to BASE class, since it is having CHILD class object in it, it will preserve the CHILD CLASS MEMORY configuration.
As a Result, by default, it will have access to the CHILD CLASS METHODS.
Hence, if you utilize this reference to call a method, which is present in BOTH THE CLASSES, the child class method will be called.
But, probably following will clear the picture for you.
Create a method in BASE class, mark it as PRIVATE and try to call that method inside static void bogo(Base b) by:
b.turn(1); //My method name is turn(int)
It will give you a compiler error saying turn has private access inside BASE.
This is because, the reference memory configuration is of child.

Let me know if what ever I said makes sense...this is just my interpretation and not take from any book or something. so I am a bit skeptical about it..

Thanks
Abhijit
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Hi Harshad,

Great reply. It is crystal clear now.

Thanks.

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what gets printed and why?
 
Similar Threads
mughal question,need help
Khalid Mock - Constructor Evaluation
Inheritance question
Constructor Problem
A Mughal question