This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Method Dispatch  RSS feed

 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In order to call B's method why i cant use A's reference variable i.e a which is initialized to B's instance and hence a is visible to B's members. But in case when meth() is overridden i could call using a.meth() without an error

 
Amarnath Pandi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you should try to change the main method's argument name!

When you say A a = new B(); you mean class A only. A doesn't have a method called meth();



From the object 'a' you cannot see the variable y.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amarnath Pandi wrote:May be you should try to change the main method's argument name!


What's wrong in main() argument name?

When you say A a = new B(); you mean class A only. A doesn't have a method called meth();


If A a = new B() doesn't mean class B, then how its mean class B when meth() is overridden in class B? So how this expression alter its behaviour and act as Dynamic Method Dispatch in case of method overriding.

Am bit confused about this behaviour. It is strange for me. Seeks proving explanation.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class does not have a meth() does it? Where is the overriding?
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your sub class overrides the methods from the base class..

Then you can use the base class reference to call the sub class methods.

What happens in ...


is compiler .. checks the implementation of A.. and tries to find if a method called meth() present in Class A. If not it throws the error.



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sidharth Pallai wrote:What's wrong in main() argument name?

It's called a in your code above, but you also have a local variable called a. Your code won't compile, because you can't have two variables with the same name in the same scope.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but you also have a local variable called a


I didn't check this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!