aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism question" Watch "Polymorphism question" New topic
Author

Polymorphism question

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Greetings there everyone.

I am reviewing chapter 8 ,Inner Classes, from Kathy Sierra and Bert Bates book , trying to spot polymorphism with inner classes, but I got confused by a simple example and I came back to review this content in the correct chapter (2) from book. Concretely , I am reviewing again page 104 example , and comparing it with the example showed in page 676 from Kathy's and Bert's book.

I do understand this applies when comes to overriding and overloading. When it comes to overriding, we do have to check out reference type, and when comes to overloading we do have to check out object type.

OK then, given the next example from page number 102 ( chapter 2) :


Some lines down it gives explanation :

In the preceding code, the test class uses an Animal reference to invoke a method
on a Horse object. Remember, the compiler will allow only methods in class Animal
to be invoked when using a reference to an Animal.


As it says this code would not compile as well :

Animal c = new Horse();
c.buck(); // Can't invoke buck();
// Animal class doesn't have that method


which is almost the same scenario as the simple example in page 675-676



OK, plain polymorphism , so I take, as book states, I have to watch for reference type. Fine.

In this last example makes sense , Animal reference but Horse object, and based in the above line, our var h has Animal reference, so there when trying to invoke buck() method fails.

Based in the above,why in the first example :




b having Animal reference, yet runs Horse version of eat() method? (Like if it were overloading instead an overriding )


Thanks in advance.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi David,

Both the examples are correct.

In first example,
Animal b = new Horse();
Here, reference is type Animal and object is type Horse.
Overriding is the Runtime-polymorphism. so overriding take place only runtime not compile time, and method call of object b depends on reference type.
here b.eat() method calls the Animal's method compile time and at runtime due to the object is type Horse , so the method override take place and runs the Horse's object...

but in your second example,
Animal h = new Horse()
h.buck() gives compile time error , because Animal doesn't have a buck() method.

Hope that will help.
Thanks,
Ankit


OCPJP 6 (91%)
Alan Cowap
Greenhorn

Joined: Oct 16, 2012
Posts: 6

David Samer wrote:Greetings there everyone.

<snip>



OK, plain polymorphism , so I take, as book states, I have to watch for reference type. Fine.

In this last example makes sense , Animal reference but Horse object, and based in the above line, our var h has Animal reference, so there when trying to invoke buck() method fails.

Based in the above,why in the first example :




b having Animal reference, yet runs Horse version of eat() method? (Like if it were overloading instead an overriding )


Thanks in advance.


Hi David,

In the second example, both classes (Animal and Horse) have an eat() method so the compiler is happy that an Animal object reference can have an eat() method invoked on it
At Runtime, the JVM can invoke this method on whatever the actual object is, whether it's an Animal or a Horse object.

However, in the second example, the compiler knows that the Animal class does not have a buck() method, so compilation fails.
The fact that the actual object referred to is a Horse (which does have a buck() method) isn't enough, because the compiler knows the Animal object reference could refer to an Animal (or any subclass of Animal) which may not have a buck() method. So you can't invoke a method buck() on a reference type that doesn't implement a method buck().

Regards,
Alan


[EDIT: Apols, Just saw Ankits answer]


This is my .sig. There are many .sigs like it, but this one is mine.
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Ankit Gareta wrote:Hi David,

Both the examples are correct.

In first example,
Animal b = new Horse();
Here, reference is type Animal and object is type Horse.
Overriding is the Runtime-polymorphism. so overriding take place only runtime not compile time, and method call of object b depends on reference type.
here b.eat() method calls the Animal's method compile time and at runtime due to the object is type Horse , so the method override take place and runs the Horse's object...

but in your second example,
Animal h = new Horse()
h.buck() gives compile time error , because Animal doesn't have a buck() method.

Hope that will help.
Thanks,
Ankit


==============

[quote=Alan Cowap
Hi David,

In the second example, both classes (Animal and Horse) have an eat() method so the compiler is happy that an Animal object reference can have an eat() method invoked on it
At Runtime, the JVM can invoke this method on whatever the actual object is, whether it's an Animal or a Horse object.

However, in the second example, the compiler knows that the Animal class does not have a buck() method, so compilation fails.
The fact that the actual object referred to is a Horse (which does have a buck() method) isn't enough, because the compiler knows the Animal object reference could refer to an Animal (or any subclass of Animal) which may not have a buck() method. So you can't invoke a method buck() on a reference type that doesn't implement a method buck().

Regards,
Alan


[EDIT: Apols, Just saw Ankits answer]

=======

First, thanks both for your answers Ankit and Alan,

Yet I am a bit confuse.

So I do understand when having something like (following the example ) Animal a = new Horse(); I do have to look carefully for what question is asking for? If I do see they are asking if code compiles ...then, am I right thinking reference type? on the contrary if I do see questions is asking if runs (executes) or not... then am I right thinking object type?

I do apologize for inconvenience it's just a little bit hard for me to get the point regarding when takes place what both have stated regarding to compilation time and run time
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
Hi David

With polymorphism when you declare the reference type in the declaration, it can be from a superclass of the object we are creating. The compiler has no problem with this but the power of polymorphism happens at runtime through the concept of virtual method invocation. This concept is the dynamic selection of overridden methods at runtime based on the actual object type, rather than the reference type. This only applies to overridden instance methods, everything else uses the reference type at runtime.

We can use the supertype for reference type declaration when instantiating our objects, safe in the knowledge that the JVM will use the actual object created to invoke the correct overridden methods of the subtypes at runtime.

The superclass doesn't know anything about the methods in the subclass so this only works one way, so an Animal reference type can be used as a declaration and at runtime the actual Horse object is used to get the right override.

Overloading is something totally different, it's just a different method with the same name and a different argument list.

For a fuller explanation try this lesson from my site Polymorphism

Regards Kevin


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

Kevin Florish wrote:Hi David

With polymorphism when you declare the reference type in the declaration, it can be from a superclass of the object we are creating. The compiler has no problem with this but the power of polymorphism happens at runtime through the concept of virtual method invocation. This concept is the dynamic selection of overridden methods at runtime based on the actual object type, rather than the reference type. This only applies to overridden instance methods, everything else uses the reference type at runtime.

We can use the supertype for reference type declaration when instantiating our objects, safe in the knowledge that the JVM will use the actual object created to invoke the correct overridden methods of the subtypes at runtime.

The superclass doesn't know anything about the methods in the subclass so this only works one way, so an Animal reference type can be used as a declaration and at runtime the actual Horse object is used to get the right override.

Overloading is something totally different, it's just a different method with the same name and a different argument list.

For a fuller explanation try this lesson from my site Polymorphism

Regards Kevin


Thank you Kevin.

The first paragraph made me understand it finally . Well explained also. And useful website , I took the test and examples for training regarding polymorphism, very useful the little test at the end

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism question