File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method Overriding" Watch "method Overriding" New topic

method Overriding

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
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

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. 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.
[ September 11, 2002: Message edited by: Barkat Mardhani ]
[ September 11, 2002: Message edited by: Barkat Mardhani ]
I agree. Here's the link:
subject: method Overriding
It's not a secret anymore!