• 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

polymorphism, overriding - confusion ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading now a SCJP5 study guide book (Osborne, 2006 - Sierra,Bates) and i am little confused.

Problem is in these two sentences : ( two minute drill chapter 2, pages 153,154)

Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.


Just imagine :

class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}



now :

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

according first sentence should be printed : 'stinky animal !'
according second sentence should be printed -> 'stinky dog !' (this is printed in reality)

Is explanation in that book wrong or i made logical mistake somewhere ?

(sorry for my english)
Thanks
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong in the explanation.



What it meant was.

when you create object like this and call a method:

Animal obj = new Dog();
obj.stinky();

What compiler does is, it check the class type of obj which is Animal. After that it checks whether the stinky() exist in Animal or not. Always remember that objects are created at runtime. So complier has no way to determine that the Dog class stinky() method is to be called. So at compile time class type of reference variable is checked to check such a method exist or not.

Now at run time the jvm knows that though the class type of obj is Animal but at run time it is referring to the object of Dog. So it calls the stinky() of Dog class. This is called Dynamic Polymorphism.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose if I change the classes to as given below.


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

As per your point 1, you wont be able to call a.bark()

as per your point 2 if you call the stinky method, it will call the stinky in Dog.

class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}
public void bark(){
System.out.println("wow wow");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}

Cheers,
Neo

lucas van derstappen wrote:I am reading now a SCJP5 study guide book (Osborne, 2006 - Sierra,Bates) and i am little confused.

Problem is in these two sentences : ( two minute drill chapter 2, pages 153,154)

Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.


Just imagine :

class Animal {
void stinky(){
System.out.println("stinky animal !");
}
}

class Dog extends Animal{
public void stinky() {
System.out.println("stinky dog !");
}

}
class Cow extends Animal{
public void stinky() {
System.out.println("stinky cow !");
}



now :

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

according first sentence should be printed : 'stinky animal !'
according second sentence should be printed -> 'stinky dog !' (this is printed in reality)

Is explanation in that book wrong or i made logical mistake somewhere ?

(sorry for my english)
Thanks

 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you wont be able to even compile in that case.
Beacuse at compile time the comiler sees that the class type of obj is Animal and there exist no method bark() in Animal class. Hence it will display a compile time error.
 
Neo Neo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, but thaz what I too said :-)

Priyam Srivastava wrote:No you wont be able to even compile in that case.
Beacuse at compile time the comiler sees that the class type of obj is Animal and there exist no method bark() in Animal class. Hence it will display a compile time error.

 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah you did
hopefully you understood the explanation
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lucas van derstappen wrote:
Chapter about Polymorphism :
(I.) Reference variable`s type (not the object`s type) determines which methods can be called.

And

Chapter about overriding :
(II.) Object type (not reference variable`s type), determines which overriden method is used at runtime.





Dear I think your question itself holds the answer.....


The reference variable, when of super type, the methods that can be called on that type is those that are defined in the super class. And when at run time which overridden method is called depends on the object to what the reference variable is actually pointing to. This is also known as dynamic bynding

Hope this helps
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically at compile time, the compiler is interested in the reference type, whilst at runtime the JVM is interested in the Object type (just in time compiling). It took me a while to get used to this, but after that its quite straight forward! Dont forget objects are only created at runtime, so before that you only have the compiler!
 
reply
    Bookmark Topic Watch Topic
  • New Topic