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

Object variable Inheritance  RSS feed

 
Justin Hawtree
Greenhorn
Posts: 3
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say you got two classes. A person class and a Worker class. The Worker class extends the person class.
So when you make the objects like this.

My question is what is the difference between the three objects made?
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p will have access to all of Person's methods and fields.
pw will have access to all of Person's inherited methods and fields, but will not have access to unique methods and fields defined in the Worker class, unless an explicit cast is made.
w will have access to all of Person's inherited methods and fields, and everything defined in the Worker class.

Inherited methods from Person that are overriden in Worker will still be called using the Worker definition with pw (polymorphism).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!