File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in inheritance

 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why does this code give output
B
B
and not
B
A
Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you need to take a(nother) look at this
 
anandkumar gangoni
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the point here is class C inherits the method show() from class B. and second point is call to a method depends on the actual object in the reference at runtime (but plz make a note the method declaration should also exist in the reference type orelse it will give compilation error).

so here a and b references have C object so the ouput will be B followed by B b'coz the method in class B which is inherited by class C and is called at runtime.

Cheers,
Anand.
 
maha devan
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela

>>B b = new C();
>>b.show();
The method invocation depends on the type of object stored rather than the variable type B.So in the above code B b = new C(), the compiler will first look for the presence of show() method in Class C,if it doesn't finds it, it starts searching in the parent class. So u get B printed in this case.

A a = new C();
a.show();
In this case also samething happens , the search doesn't depend on variable stored but the type of object referred.

cheers
Maha
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic