aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method Overriding" Watch "method Overriding" New topic
Author

method Overriding

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi!
can someone explain in detail what is geing checked at compile time at waht is being done at run time well my version is
that at compile time its checked wheather eat method is in MAmmal . then at runtime the actual object h denotes is of class Cattle.
so the eat() of Cattle shoulb be called.also c is of type Cattle so it should print Cattle version


thanx.
madhur.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Madhur:
Please note following:
1. eat() is NOT overridding in derived classes as arguments are of different types in each eat().
2. h is of M type but holds C type object.
3. h.eat(c) is passing C type object to eat method. That particular method ( eat(C c) ) is not available in M. As it is not in super class (i.e. M), the derived class's ( eat(C c) ) method can not be called.
4. Now M has a method with same name but different argument type. Therefore, that method ( eat(M m) ) is closest to call i.e. ( eat(C c) and it will be invoked. And you get mammal output.
Hope this helps.
Barkat
[ September 11, 2002: Message edited by: Barkat Mardhani ]
[ September 11, 2002: Message edited by: Barkat Mardhani ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: method Overriding