INHERITANCE is a mechanism that allows for the creation of new classes based on existing classes. This facilitates code reuse and the organization of classes into hierarchies.
In this context, “inheriting” means that classes (or types) have available some of the characteristics of those they inherit from. Like in the biological sense 👪.
Inheritance: This child has inherited the same hair as their mother!
In programming, inheritance is a mechanism by which some classes can copy attributes and methods from another class.
We will call,
- Child class or subclass: The class that inherits
- Parent class or superclass: The class that is inherited from
For example, if we have a parent class Animal, we could have child classes like Dog, Cat, Bird, Cow, etc.

In addition to copying the parent class’s methods, the child class can add new attributes or methods, or even override the inherited ones (otherwise inheritance wouldn’t be very interesting).
In this way, a child class can extend and specialize the behavior of the parent class. It copies part of the behavior but adds “its own details” and customizations.
When to Use Inheritance
The most common way to identify a case of INHERITANCE between classes is to detect that they share common code and we want to avoid copying it.
For example, imagine we are working on a school management system. We have a Person class.
class Person
{
string Name;
string Surname;
// many more things
}
Now we need the classes Student, Teacher, Guardian, Principal. But they all have Name and Surname. So, instead of copying it, we can make them inherit.
class Student extends Person
class Teacher extends Person
class Guardian extends Person
class Principal extends Person
Simply by doing that, our four child classes Student, Teacher, Guardian, Principal have access to Name and Surname, because they inherit them from Person.
// type Student inherits from Person
class Student
{
// 👇 this one has it, without needing to define it
// string Name;
// string Surname;
}
Indeed, avoiding code repetition is one of the main purposes of inheritance. Therefore, it is one of the first indicators that we might have a case of inheritance.
But it gets much more interesting. The real reason to use inheritance is to identify that we have a specification of the parent class (a specification is a more concrete case of a concept).
That is, Person is a broader concept than Student or Teacher. These students and teachers are particular cases of people, but they ARE people.
Inheritance is a relationship where the child class is a more specific version of the parent class.
You can detect it if you can construct a sentence containing “is a”.
- A teacher is a person
- A bird is an animal
- A car is a vehicle
- A shoe is a product
Inheritance Example
Let’s see what the syntax would be for implementing inheritance between classes in different programming languages.

Imagine you have to draw geometric shapes on a 2D video game monitor. For that, we have a parent class Shape. From it inherit Rectangle, Circle, and Triangle.
In C#, inheritance is achieved using the : symbol. When a class inherits from another, the child class acquires all the fields and methods of the parent class.
// parent class
class Shape
{
public Point2D Position;
public int Rotation;
public int Width;
public int Height;
}
// child classes that inherit
class Rectangle : Shape { }
class Circle : Shape { }
class Triangle : Shape { }
In C++, inheritance is implemented using the keyword public followed by :. This public inheritance relationship allows the public members of the base class to be accessible in the derived class.
// parent class
class Shape
{
public:
Point2D Position;
int Rotation;
int Width;
int Height;
};
// child classes that inherit
class Rectangle : public Shape {};
class Circle : public Shape {};
class Triangle : public Shape {};
In JavaScript, inheritance is achieved using the extends keyword, which allows a child class to inherit from a parent class.
// parent class
class Shape {
constructor() {
this.Position = new Point2D();
this.Rotation = 0;
this.Width = 0;
this.Height = 0;
}
}
// child classes that inherit
class Rectangle extends Shape { }
class Circle extends Shape { }
class Triangle extends Shape { }
In Python, inheritance is achieved by placing the base class name in parentheses after the child class name. This indicates that the child class inherits from the base class.
Because Python is like that and you have to love it as it is (I guess).
# parent class
class Shape:
def __init__(self):
self.Position = Point2D()
self.Rotation = 0
self.Width = 0
self.Height = 0
# child classes that inherit
class Rectangle(Shape):
class Circle(Shape):
class Triangle(Shape):
In all these examples, the child classes Rectangle, Circle, and Triangle have all the attributes and methods of the parent class Shape because they inherit from it.
Where Does Inheritance Come From?
Why was the concept of INHERITANCE invented, as a fundamental part of Object-Oriented Programming? Let’s go back to basics once again and remember that in those days, it was already common to have groupings of variables that abstracted concepts.

However, anyone could come and modify anything. Basically, you could have a Shape object, and by changing its forms, you could make it work for a rectangle, a circle, or a triangle.
But we already said that led to unmaintainable messes. That’s why Object-Oriented Programming puts ENCAPSULATION as one of its pillars. With this, no one could come and modify an object.
Now they had “fortified” the OBJECTS; they could no longer be modified as before. Now you necessarily had to repeat the code for Rectangle, Circle, and Triangle, each with all the entire code. That wasn’t very desirable either.
For that reason, INHERITANCE was invented. To allow a mechanism that enabled us to reuse code between classes, so it wouldn’t have to be copied over and over again.
But this time, doing it in a regulated and controlled way, corresponding to a kinship relationship.
