• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Polymorphism  RSS feed

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





[Fixed formatting and line-length]
I know the statement calling new method with reference variable of BaseClass is wrong, because we can invoke only overridden methods of ChildClass, and not the new methods. I want to know why is it so ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't know that on line 7 base happens to be referencing an instance of ChildClass. All it knows is that you declared the variable as BaseClass. So the only methods it knows it can safely call are those declared in BaseClass.

Consider this code:
This is the same, as far as the compiler is concerned. But what would happen if this was allowed? The addVar() method doesn't exist. A type-safe language like Java prevents you making this kind of error - but it can only do that by disallowing the call entirely. A dynamic language (like Ruby, for instance) would allow it but then throw an error at runtime if the method didn't exist.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The compiler doesn't know that on line 7 base happens to be referencing an instance of ChildClass. All it knows is that you declared the variable as BaseClass. So the only methods it knows it can safely call are those declared in BaseClass.


If compiler doesn't know, that it is referring to a reference variable of a child class, then how come it exactly pick the child class's overridden method when it should have chosen base class method. But here it choses child class's overridden method, however, it doesn't work for other methods.
Your answer seems to be contradicting to your point.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:If compiler doesn't know, that it is referring to a reference variable of a child class, then how come it exactly pick the child class's overridden method when it should have chosen base class method.

The compiler doesn't decide which method gets called at runtime. Looking up which method has to be called is done by the JVM, when the program runs, not at compile-time.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The compiles doesn't decide which method gets called at runtime. Looking up which method has to be called is done by the JVM, when the program runs, not at compile-time.

@Rubbai: Indeed, IMO "Dynamic Polymorphism" is a tautology, because polymorphism can't be anything but dynamic.

I know that it's probably used to distinguish it from "Static Polymorphism", which is what some people call method overloading; but to me that term is also wrong.

Winston
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Indeed, IMO "Dynamic Polymorphism" is a tautology, because polymorphism can't be anything but dynamic.

I know that it's probably used to distinguish it from "Static Polymorphism", which is what some people call method overloading; but to me that term is also wrong.

Winston


So, you mean Static polymorphism is method overloading and Dynamic polymorphism is method overriding.
Is it so ?
 
milan chovatiya
Greenhorn
Posts: 24
Java Oracle Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@rubbal its not 100% but 99% its true....static overloading......dynamic overriding
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!