• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance doubt

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of:

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and why?
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much:)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic