Skip to main content

Inheritance

In Turkish
Kalıtım
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/inheritance

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.
  • super calls 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

A subclass that extends and overrides its parent in Pythonpython
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))  # True

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings