Inheritance (computer science)

In Object-oriented programming languages, inheritance is a way to add functionality. Object-oriented programming has the notion of classes (and perhaps interfaces). A derived class inherits most fields and methods from its parent class. It can modify the behaviour of the parent, by adding new fields and methods, or by modifying existing ones. Depending on the programming language used, there may be certain restrictions when extending a class.

Inheritance can be used to solve different types of problems:

  • Specialisation: The child class extends the functionality of the parent. That way a Bank account may have fields for balance, account number and owner; a subclass interest-earning account may add fields like interest rate and interest received.
  • Overriding: The child class can redefine the behaviour of the parent class.
  • Code reuse: Code that is common to many classes can be placed in a parent class, and reused.

An example can be a Car class. You could create a Ferrari class which would inherit from Car, and also make a Toyota class which would also inherit from Car. This way, all the fields and methods (drive method, brake method, color field...) would also be inside of Ferrari and Toyota, and you wouldn't need to copy the code twice. However, maybe inside of Car you could have a brand field. This brand field would be equal to nothing, but inside of Ferrari the field might be equal to "ferrari" and in Toyota to "toyota". Of course, you could not just create a Car, because otherwise that field would be missing. In order to prevent people from just creating a Car, you turn the Car into an abstract class.

Other Languages
العربية: وراثة (حوسبة)
azərbaycanca: İrsilik
беларуская (тарашкевіца)‎: Спадкаваньне (праграмаваньне)
latgaļu: Bērnaklase
norsk nynorsk: Arv i programmering