Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritence

 
Mrunal Bandodkar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

During inheritence,say
class a
class b extends a
suppose if we create objects a A=new b();
A.print();
then should the print method occur in both the classes or if it is present in one that's ok.
Is there any rule or anything like that
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the way you create your object, both classes need to have that method print() otherwise a compile-time error occurs (in class a) which says "cannot resolve symbol print()". This is because the method to execute will be resoved a runtime according to some specific rules (see JLS 15.12 for complete information about that)
The method that finally gets invoked in this case is the one in class b because the object is of type b (although being referenced by a variable of type a)
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 28, 2001).]
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mrunal Bandodkar:
hi all,

During inheritence,say
class a
class b extends a
suppose if we create objects a A=new b();
A.print();
then should the print method occur in both the classes or if it is present in one that's ok.
Is there any rule or anything like that

either class can have the method print();
or if both have it then class B override class A's print();
there is one rule though, with:
A=new b();
A.print();
if class A doesn't have the method print() there will be compiler error. With that code, compiler see that the reference A is class A and it doesn't have method print() even thought at runtime the method that will be called is the actually Object.
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a A=new b();
The compiler knows that the type of the object A is a. So, when you call A.print(), it will check whether the class of A (i.e, class a ) has defined this method or whether it has inherited that method from its superclass. If not, then it will throw the compiler error.
So in this case, the class a should have print() method.
Then at run time, the real class of the object A is b. So, it will check (whether the class b has defined that method ) or (whether it has inherited from its superclass, if not defined in class b). If it could not find any method, then it will throw
RuntimeException "java.lang.NoSuchMethodError"
Hope this helps...
Uma
 
Mrunal Bandodkar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx everybody,
one more question:
if i create object using b B=new b();
b.print();
then if print not present in class b then it will execute print method in A,am i right?
And if i ccreate another class
class test{
public static void main(String args[]){
a A=new b();
a.print;
)
Here also print should be presnt in class a and b,
correct me if i sm wrong
thankyou so much
once again
Mrunal
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first question:
if i create object using b B=new b();
b.print();
then if print not present in class b then it will execute print method in A,am i right?
YOU ARE RIGHT, if class a has print(). It will invoke the inherited version from class a
Your second question:
class test{
public static void main(String args[]){
a A=new b();
a.print;
)}
Once again you are confused.
class a should have print(). Otherwise, you will get compiler error.
class b does not need to have print() but it has one, it will invoke that method. Otherwise, it will invoke the inherited version from class a
[I think that this question is the same as your original question...]
Uma
 
Mrunal Bandodkar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uma got it,
thankx alot.
So it is necessary for 'a' to have print method and 'b' may or maynot have it ,if it does not have it will inherit from super class provided it is not declared private else compile time error.
Am i right
Thanks once again
Mrunal
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes buddy, you got it...
Uma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic