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

inheritance question  RSS feed

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey yall, nice to be back. Got a quick question.
When do you use inheritance and when do you use an interface to create a polymorphic reference?
Thanks in advance
Steve
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey SteveO,
Well, you generally use an interface to define a common behavior among unrelated classes. For example if you had a Person class and a Car class with no common ancestors, they could both implement an interface Wash since both occasionally need to knock off the trail dust.
Inheritance or extending a class on the other hand involves classes with similar state and behavior, for example, Vehicle, Automobile, Car, etc. Each inherited level usually adds either more state or behavior.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike
That Lone star is on the way

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!