• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in K&B SCJP 5: topic - Overriding

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt about reference variables and method overriding.

On page 98, it says:

Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type.

But in the Exam Watch column on page 104, it says:

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you�re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception(more in Chapter 5).

Which of these is right?
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Just noticed this below the code sample of the above Exam Watch column.

This code will not compile because of the Exception declared on the Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception.

My doubt has been partly cleared, but I'm still confused. The method which is executed at runtime apparently belongs to the object's class, but what role does the compiler's assumption play here? Could someone explain the concepts involved?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of overriding, method called according to object type, not reference type.

Ex:
class Animal
{
void A(){}
}
class Dog extends Animal
{
void A(){}
}
now if you create a object of Dog like this

Animal a = new Dog();
a.A();

In this case, Dog's A method will get called, not animal's method.

So in case of overiding, always check object type
and in case of overloading, always check refernce type

Hope it clears your doubt.
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Gunjan! From your example, I understood that the subtype's overridden method is called during runtime. But I have another doubt. The book says:

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you�re calling the supertype version of the method.



Why does the compiler assume that the supertype version is called? How is it significant?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mithun Mohandas:
Why does the compiler assume that the supertype version is called? How is it significant?


The compiler must check the syntax of the program, and in this case, the supertype is the only thing the compiler knows about the object.
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks Manfred!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal
{
void A(){}
}
class Dog extends Animal
{
void A(){}
}
now if you create a object of Dog like this

Animal a = new Dog();
a.A();

Hi Mithun always remember that for overriding the compiler always look
at the object type and this is done dynamically

in the above dogs method will be called,but if in case there is no such
method in dog then the method with reference type will be called
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Zartab!
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mithun,
i am still not clear with this.
Can you please explain?
The concept i am not clear about is: its a valid overidden method, then why is it that we will get a compiler error?
Can you explain me different scenarios over where we will be expecting a compiler error and where we will not

Thanks in advance....
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Debasmita!

I assume you are talking about the code in the Exam Watch section which I referred to earlier. This is how I understand it:



At runtime, both d.eat() and a.eat() will end up executing the same method, that is, the overridden version in the Dog2 class. The difference between the two function calls is the TYPE of the OBJECT REFERENCE used.

At compile time, the compiler assumes that a.eat() will call the eat method of the Animal class (since 'a' is an Animal reference) and that d.eat() will call the overridden version of the method in the Dog2 class (since 'd' is a Dog2 reference). So, even though a.eat() will call only the Dog2 version of the method at runtime, the compiler expects an appropriate function call that corresponds to the Animal version of eat(), which throws an exception. Therefore, the above code will compile if we enclose a.eat() in a try-catch block. BUT, I repeat, at runtime, only the Dog2 version of eat is called by a.eat(). This whole exception handling process we perform is merely for the compiler's satisfaction.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mithun,
Thanks a lot....
i got it....
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mithun Mohandas:
This whole exception handling process we perform is merely for the compiler's satisfaction.



In this case, yes. But in the general case, you will use an Animal reference if you don't know which objects you get at runtime. And as result, you must be prepared to handle the exception.
 
Mithun Nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I got it. Thanks again, Manfred!
 
reply
    Bookmark Topic Watch Topic
  • New Topic