aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: topic - 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 "Doubt in K&B SCJP 5: topic - Overriding" Watch "Doubt in K&B SCJP 5: topic - Overriding" New topic
Author

Doubt in K&B SCJP 5: topic - Overriding

Mithun Nair
Greenhorn

Joined: May 12, 2007
Posts: 9
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

Joined: May 12, 2007
Posts: 9
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?
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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.


Gunjan Kumar
SCJP 1.5
Mithun Nair
Greenhorn

Joined: May 12, 2007
Posts: 9
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?
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: May 12, 2007
Posts: 9
Got it. Thanks Manfred!
zartab ameen
Greenhorn

Joined: May 01, 2007
Posts: 13
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

Joined: May 12, 2007
Posts: 9
Thanks a lot Zartab!
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
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....


debasmita
Mithun Nair
Greenhorn

Joined: May 12, 2007
Posts: 9
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

Joined: Apr 12, 2007
Posts: 94
Hi Mithun,
Thanks a lot....
i got it....
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: May 12, 2007
Posts: 9
Yeah I got it. Thanks again, Manfred!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in K&B SCJP 5: topic - Overriding