Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance doubt

 
Jeet Jain
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output of:

 
Jeet Jain
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why?
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ket bhav
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic