aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritence" Watch "Inheritence" New topic
Author

Inheritence

Mrunal Bandodkar
Greenhorn

Joined: Oct 09, 2001
Posts: 6
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


mnb
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
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

Joined: Jun 14, 2001
Posts: 126
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

Joined: Oct 09, 2001
Posts: 6
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

Joined: Jun 14, 2001
Posts: 126
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

Joined: Oct 09, 2001
Posts: 6
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

Joined: Jun 14, 2001
Posts: 126
Yes buddy, you got it...
Uma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritence