This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance doubt " Watch "Inheritance doubt " New topic
Author

Inheritance doubt

Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
What is the output of:

Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
and why?
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Jeet Jain wrote:What is the output of:

Why don't you compile and run it? and then tell according to you what the output should be and why it should be that.


Piyush
ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22
When

B ref2 = (B) ref1;

reference ref1 will be cast to class B.
so B's method will be called.

And class B's

public int g() { return f(); }

will call method of class B.

if that might not exist then an only it will try to find from super class that is class A.
But in this case f() exist in class B so it will return 1.
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
but arent method calls determined based on actual type of object and not its reference type? which is why i got confused. Actual object is C so it should call C's f()
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Jeet Jain wrote:but arent method calls determined based on actual type of object and not its reference type?

This happens only in the case of overriding. Here method f() is private in both A and B classes so C is not overriding f().
gihan dissanayaka
Greenhorn

Joined: Sep 03, 2011
Posts: 8
ket bhav wrote:When

B ref2 = (B) ref1;

reference ref1 will be cast to class B.
so B's method will be called.

And class B's

public int g() { return f(); }

will call method of class B.

if that might not exist then an only it will try to find from super class that is class A.
But in this case f() exist in class B so it will return 1.

dear ket, i think when you dide casting you are not pointing to that class see that below it can still access to sub class. i think what happens is it only limited to the methods which caseted class have
ket bhav
Greenhorn

Joined: Aug 28, 2011
Posts: 22
Hey Gihan,

A perfect example you gave, it cast to B but actual object of C so it called tried to call method g() of class C, but it could not found and due to overriding it wen to g() of class B and from B it called it's own method f() and if it was not available it will check from super class - class A and if exist then execute it.
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
thank you so much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance doubt
 
Similar Threads
access modifier
out put of class
Polimorphism x Casting
Overriding
Dynamic Method Lookup