Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method Overriding

 
madhur jain
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic