• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Polymorphism question

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
The City calls upon her steadfast protectors. Now for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic