• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about polymorphism  RSS feed

 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm quite new to programming and I just read the nice campfire story on JavaRanch.
I really liked the story, but there is one thing I keep puzzled about. Imagine the following code which I borrowed from the story above:

I understand that I can have an object type which is a class or subclass of the reference object. So the code below will be legal:
Also I follow that at run-time Java select the method from the dog object so if you call d.play() it will call the method from the Dog class.
But since it calls the method from the Dog class, why is it not possible to call other methods like d.bark()? Since it knows about the Dog-class, why wont it be able to call the dog-specific methods on it?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't know that d holds a reference to a Dog, because d is of type Animal. Not all animals can bark(), so calling that method is illegal.

Determining what method signature belongs to a method call is done at compile time, and depends on the formal type of the variable. This is also known as static or early binding.

Determining what method implementation belongs to a method call is done at runtime, and depends on the actual type of the object referenced by the variable. This is also known as dynamic or late binding.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example of early binding is when the method being called depends not on the formal type of the variable the method is being called on, but the formal type of the argument passed to the method call:

Even though the actual type of pupper is Dog, its formal type is Animal, so the playWith(Animal) overload is being called. Remember, first the method signature is determined at compile time, based on the formal types of the variable and arguments involved. Then, at runtime the exact implementation for that method signature is determined based on the actual types of the objects involved.
 
O Shea
Ranch Hand
Posts: 146
2
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you encounter such type of Polymorphism/Overriden Questions then make judgement like->

Here you have->
Now d is reference var. of type Animal but contains object of type Dog(that we see later since it occurs at Runtime).

Now while making any call from Animal reference variable say-> d.play() //Here see whether d contains any play() method->yes(because d is of which type ->Animal , ok is Animal class have any method whose name is play()->Yes . Fine then compiler is happy that he founds that method.

Now see->
Here compiler check that d is of which type->Animal .Ok. Then whether Animal contains any method whose name is bark()? -> NO , Animal has only 1 method whose name is play() and immediately Compiler becomes unhappy and give Compile Time Error saying that cannot found any method named bark() in Animal class.

These concept is called Early Binding since at compile time these all checkings are going on . Runtime checkings are handled by JVM.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!