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

should you always program with inheritance in mind?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i know silly little programs dont require you to use inheritance...you can just program using a few classes etc
but where do you draw the line?
when does it become necessary to start using inheritance?
am i right in saying that inheritance down the line will make it easier to scale a project...adding things and changing things will be easier if using inheritance?
is it something i should start implementing from the get go?
any info appreciated

  
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inheritance is a tool, it has its places where it's the right tool for the job.

In very basic terms, when you read through your requirements look for:

  • NOUNS These will become classes
  • VERBS These will become methods
  • IS-A relationships will become inheritance
  • HAS-A relationships will become aggregations

  • A car IS-A vehical, for instance - inheritance.
    A car HAS-A engine - aggregation.

    Among other things, inheritance reduces code duplication.
    Inheritance is a way of implementing polymorphism.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you shou‍ld be practising inheritance from early stages in your career. Unfortunately, you need to know about things like the Liskov Substitution Principle. More details here. Note the classic counter‑example of the square and rectangle.
    You will unfortunately learn with experience that in many circumstances, inheritance causes more harm than good.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Somebody said they were having difficulties opening the first link in my previous post. Maybe this pdf will be easier to open.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!