Inheritance
- In Turkish
- Kalıtım
In short
Inheritance is an object-oriented programming feature that lets a new class reuse, extend, and override the fields and methods of an existing class.
What is inheritance in programming?
Inheritance lets you create a new class based on an existing one. The new class, called the child class or subclass, automatically gets the fields and methods of the parent class, also called the base class or superclass, and can add its own or replace existing ones. It models an is-a relationship: a Dog is an Animal, so Dog can inherit from Animal.
When you call a method on an object, the language first looks for it in the object's own class and then walks up the chain of parent classes until it finds it. A subclass can override a method, meaning it provides its own version with the same name, and it can still call the parent's version with a keyword such as super. Java, C#, and JavaScript allow only one parent class, while C++ and Python allow a class to have several.
Think of a family recipe passed down through generations: each cook inherits the original but may swap an ingredient or add a step. Inheritance is common in UI frameworks, game engines, and standard libraries, for example when you create a custom error type that extends a built-in Error or Exception class.
Inheritance is often compared with composition, where a class holds other objects and delegates work to them instead of extending a parent. Deep inheritance trees create tight coupling, since a change in a parent can break every child, so a common guideline is to favor composition over inheritance and save inheritance for true is-a relationships. Inheritance also differs from implementing an interface, which shares a contract but no code.
Key takeaways
- A child class inherits the fields and methods of its parent class.
- Subclasses can add new members and override inherited methods.
supercalls the parent's constructor or its version of a method.- Use it for true is-a relationships; prefer composition for has-a relationships.
- Most languages allow only one parent class; C++ and Python allow several.
Example
class Animal:
def __init__(self, name):
self.name = name
def describe(self):
return f"{self.name} is an animal"
class Dog(Animal): # Dog inherits from Animal
def __init__(self, name, breed):
super().__init__(name) # run the parent's constructor
self.breed = breed
def describe(self): # override the parent's method
return super().describe() + f" ({self.breed})"
print(Dog("Rex", "beagle").describe()) # Rex is an animal (beagle)
print(isinstance(Dog("Rex", "beagle"), Animal)) # TrueReaders ask
What is the difference between inheritance and composition?
Inheritance creates an is-a relationship where a class extends a parent and reuses its code. Composition creates a has-a relationship where a class contains other objects and delegates work to them, which is usually more flexible and less tightly coupled.
What is multiple inheritance?
Multiple inheritance means a class has more than one parent class. C++ and Python support it, while Java and C# allow only one parent class but let a class implement many interfaces.
What is method overriding?
Overriding means a subclass defines a method with the same name and parameters as one in its parent, replacing the parent's behavior for objects of the subclass. The subclass can still call the original version through super.
See also
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- PolymorphismProgramming Fundamentals, p. 45Polymorphism is the ability of code to work with values of different types through one shared interface, with each type supplying its own behavior.
- InterfaceProgramming Fundamentals, p. 29An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
- EncapsulationProgramming Fundamentals, p. 17Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
- ConstructorProgramming Fundamentals, p. 13A constructor is a special method that runs when a new object is created from a class, setting up its starting values so it is ready to use.
Spotted a mistake or something missing on this page?Suggest an edit