This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Dynamic Polymorphism

 
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 ?
 
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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 ?
 
Greenhorn
Posts: 24
Python Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@rubbal its not 100% but 99% its true....static overloading......dynamic overriding
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic