• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Superclass and subclass reference type confusion

 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have confusion regarding the output which is: sub1 2
But i can't understand why obj.i prints value 1 rather than 2
as the actual type of obj is B at run time ( it calls obj.myInt of class B???). So it must print sub2 2.
Not getting overriding concept right.Confused what gets called subclass /super class method depending on reference type.
Below is the code




 
Henry Wong
author
Marshal
Pie
Posts: 21225
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
supriya riya wrote:
I have confusion regarding the output which is: sub1 2
But i can't understand why obj.i prints value 1 rather than 2
as the actual type of obj is B at run time ( it calls obj.myInt of class B???). So it must print sub2 2.
Not getting overriding concept right.Confused what gets called subclass /super class method depending on reference type.


Polymorphism only applies to method calls -- and specifically, only non-static method calls where overriding is taking place. For variables (either static or instance) and for other types of methods calls (such as static methods or private methods), it is completely resolved at compile time.

Henry
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic