Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and type of variables when invoking a method  RSS feed

 
negin nafari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all,

I have some big issue understanding the result of some code.


And the main that looks like this

  

I'm having a hard time understanding why the result for a1.stampa(b1); is "AAA/BBB" and not "BBB".
As i did understand from inheritance the static type of a1 in compile time is ClassB so i searche for the "stampa" method in ClassB, and for the parameter of the method, the static type is ClassB also, so I will choose the first stampa method of the ClassB.

The same thing also happens when i try to understand the result of a2.stampa(c1); which is "AAA/CCC" and not "CCC".

Can someone please help me to understand what i'm doing wrong?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
negin nafari wrote:
As i did understand from inheritance the static type of a1 in compile time is ClassB so i searche for the "stampa" method in ClassB, and for the parameter of the method, the static type is ClassB also, so I will choose the first stampa method of the ClassB.


You may be allowed to do this, but unfortunately, the compiler is not. At compile time, which the compiler run in, all the compiler has is the reference type. This means that it only knows about the methods of the superclass.

Polymorphism states that when running a method of a certain signature, the overridden version is to be called. It does not state that this must apply when determining the method to call in the first place.

Henry
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
negin nafari wrote:
As i did understand from inheritance the static type of a1 in compile time is ClassB


This is incorrect. The static type of a1, i.e the type that the variable a1 was declared with, is ClassA. This is the only type the compiler cares about.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a reason why the compiler can only go on the declared type of the object. You can always change the runtime type of the object. Look at this post from last week. Is the Animal going to be a lion or a wildebeest? What will happen if you call
x.eatMeat();
after that bit of code? Will a Lion object have such a method? Will a Wildebeest object have such a method? Will such code run correctly? The only way to be safe is to permit only methods in the Animal class. Any other methods shou‍ld fail to compile.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!