
Inheritance in OOP
Inheritance is one of the most important concepts in Object-Oriented Programming. It allows one class to receive properties and methods from another class, making it easier to reuse code, organize related logic, and build flexible software systems.
Instead of writing the same code again and again in different classes, inheritance helps developers create a shared structure. A general class can define common behavior, while more specific classes can extend that behavior and add their own features.
Introduction
In the previous articles of this OOP series, we introduced classes, objects, properties, methods, constructors, and destructors. These concepts help us understand how objects are created and how they store data and behavior.
Inheritance builds on these ideas by showing how classes can be connected to each other. In real software projects, many objects are related. For example, a user, an admin, and an editor may all share common information such as name, email, and password. However, each one may also have different permissions and responsibilities.
Using inheritance, we can place the shared logic in a parent class and allow other classes to inherit from it. This makes the code cleaner, easier to maintain, and more organized.
What Is Inheritance in Object-Oriented Programming?
Inheritance in OOP means that a class can use the properties and methods of another class. The class that provides the shared code is usually called the parent class, base class, or superclass. The class that receives and extends this code is called the child class, derived class, or subclass.
The main idea is simple: if several classes share common behavior, we do not need to duplicate the same code in each class. Instead, we define the common behavior once in a parent class, then let child classes inherit it.
For example, a general Person class may contain properties such as name and email. A Student class and a Teacher class can inherit from Person because both are types of people. Each child class can then add its own specific properties and methods.
Why Inheritance Is Important
Inheritance is important because it supports code reuse and better software organization. Without inheritance, developers may repeat the same logic in many different places. This can make the application harder to update and more likely to contain bugs.
When common logic is placed in a parent class, changes can be made in one place. If a method needs to be improved or a shared property needs to be updated, the developer can modify the parent class, and the child classes can benefit from the change.
Inheritance also helps represent real-world relationships in code. Many systems contain objects that have a general type and more specific subtypes. OOP inheritance allows this relationship to be modeled clearly.
Parent Class and Child Class
The parent class contains the shared structure. It can include properties, methods, constructors, and other logic that should be available to child classes. The child class inherits from the parent class and can use its public or protected members depending on the programming language and access modifiers.
A child class can also define additional properties and methods that belong only to that child class. This allows the child class to be more specific while still keeping the shared logic from the parent class.
For example, a parent class called Employee may contain a name, email, and salary. A child class called Manager may inherit those features and add a method for approving reports. Another child class called Developer may inherit the same employee data and add a method for writing code or managing tasks.
Simple Example of Inheritance
Consider the following basic example:
class User {
public string $name;
public string $email;
public function login() {
return $this->name . " logged in.";
}
}
class Admin extends User {
public function deletePost() {
return "Post deleted by admin.";
}
}
In this example, the User class is the parent class. It contains the name, email, and login method. The Admin class extends User, which means Admin inherits the properties and methods of User.
An Admin object can use the login method from the User class, and it can also use its own deletePost method. This is the power of inheritance: the child class keeps the shared functionality and adds more specific behavior.
The extends Keyword
Many programming languages use a keyword to define inheritance. In PHP, Java, and several other languages, the keyword extends is used to make one class inherit from another class.
The structure usually looks like this:
class ChildClass extends ParentClass {
// child class code
}
This means that ChildClass receives the accessible properties and methods of ParentClass. The child class can then add new behavior or customize inherited behavior when needed.
Although the exact syntax may change between programming languages, the concept is the same: inheritance creates a relationship between a general class and a more specific class.
Code Reuse with Inheritance
One of the main benefits of inheritance is code reuse. When multiple classes need the same functionality, that functionality can be placed in a parent class instead of being copied into each child class.
For example, in a web application, different types of users may need to log in, log out, update their profile, or change their password. These common actions can be placed in a general User class. Then Admin, Customer, Author, and Editor classes can inherit from User and add their own unique features.
This approach reduces duplication and makes the application easier to update. If the login logic changes, the developer can update it in the User class instead of editing every user-related class separately.
Method Overriding
Inheritance does not only allow child classes to reuse methods. It also allows them to customize behavior. This is called method overriding.
Method overriding happens when a child class defines a method with the same name as a method in the parent class. The child version replaces or customizes the parent version for that specific child class.
Example:
class User {
public function getRole() {
return "User";
}
}
class Admin extends User {
public function getRole() {
return "Admin";
}
}
Here, the Admin class overrides the getRole method. A normal User object returns User, while an Admin object returns Admin. This allows different objects to respond differently while still sharing a common structure.
Calling Parent Methods
Sometimes a child class needs to customize a method but still use part of the parent method. Many programming languages allow the child class to call the parent version of a method.
In PHP, this can be done using parent::. For example:
class User {
public function getInfo() {
return "Basic user information";
}
}
class Admin extends User {
public function getInfo() {
return parent::getInfo() . " with admin permissions";
}
}
In this example, the Admin class does not completely ignore the parent method. It uses the parent getInfo method and adds extra information. This is useful when the child class needs to extend existing behavior instead of replacing it fully.
Access Modifiers and Inheritance
Access modifiers control which properties and methods can be accessed from outside the class or from child classes. The most common access modifiers are public, protected, and private.
Public members can usually be accessed from anywhere.
Protected members can usually be accessed inside the class and inside child classes.
Private members can usually be accessed only inside the class where they are defined.
In inheritance, protected members are especially useful because they allow child classes to use internal parent data without making that data fully public to the outside world.
Private members are not directly available to child classes in many languages. This helps protect internal implementation details and supports encapsulation.
Types of Inheritance
Different programming languages support different types of inheritance. The most common type is single inheritance, where one child class inherits from one parent class.
Some common inheritance types include:
Single inheritance: one child class inherits from one parent class.
Multilevel inheritance: a class inherits from a child class, creating a chain of inheritance.
Hierarchical inheritance: multiple child classes inherit from the same parent class.
Multiple inheritance: one class inherits from more than one parent class. This is not supported directly in some languages such as PHP and Java classes.
Even when multiple inheritance is not supported directly, languages may provide alternatives such as interfaces or traits. These tools help share behavior while avoiding some of the complexity of multiple inheritance.
Inheritance vs Composition
Inheritance is useful, but it should not be used for every relationship. A common question in software design is whether to use inheritance or composition.
Inheritance represents an is-a relationship. For example, an Admin is a User, and a Car is a Vehicle. Composition represents a has-a relationship. For example, a Car has an Engine, and a User has a Profile.
If the relationship is clearly is-a, inheritance may be suitable. If the relationship is has-a, composition is usually a better choice. Using inheritance incorrectly can make the system harder to understand and more difficult to change later.
Real-World Example
Imagine an online learning platform. The platform may have different account types such as Student, Instructor, and Admin. All of them share basic account information, such as name, email, password, and login behavior.
A parent class called Account can contain the shared data and common methods. Then Student, Instructor, and Admin can inherit from Account. Each child class can add its own behavior:
Student can enroll in courses and submit assignments.
Instructor can create lessons and review submissions.
Admin can manage users and control platform settings.
This structure keeps the shared logic in one place while allowing each account type to have its own responsibilities.
Benefits of Inheritance
Inheritance provides several important benefits in Object-Oriented Programming:
It reduces code duplication by moving shared logic to a parent class.
It improves maintainability because common behavior can be updated in one place.
It creates a clear relationship between general and specific classes.
It supports method overriding, allowing child classes to customize behavior.
It helps build organized and scalable software structures.
These benefits make inheritance useful in many real software projects, especially when several classes share common behavior.
Common Mistakes When Using Inheritance
Although inheritance is powerful, beginners often use it too much or use it in the wrong places. One common mistake is creating inheritance relationships only to reuse code, even when the classes are not logically related.
Another mistake is creating very deep inheritance chains. When a class inherits from another class, which inherits from another class, and so on, the code can become difficult to follow. Deep inheritance can make debugging and maintenance harder.
A third mistake is placing too much logic in the parent class. If the parent class becomes too large, child classes may inherit behavior they do not really need. This can make the design less clean and less flexible.
Best Practices for Using Inheritance
To use inheritance effectively, developers should apply it carefully and only when the relationship between classes is clear.
Useful best practices include:
Use inheritance when there is a true is-a relationship.
Keep parent classes focused on shared behavior only.
Avoid very deep inheritance chains.
Use protected members carefully and avoid exposing too much internal logic.
Prefer composition when the relationship is has-a instead of is-a.
Use interfaces when different classes need to follow the same contract without sharing the same parent class.
Following these practices helps developers benefit from inheritance without creating unnecessary complexity.
Inheritance in Modern Frameworks
Inheritance is used in many modern programming frameworks. In web development, developers often extend base controller classes, model classes, or service classes. For example, a controller may inherit shared request-handling behavior from a base controller.
In PHP frameworks such as Laravel and Symfony, inheritance can appear in controllers, models, form classes, command classes, and test classes. However, modern frameworks also use composition, interfaces, traits, and dependency injection to avoid overusing inheritance.
This means that understanding inheritance is important, but developers should also learn when not to use it. Good software design depends on choosing the right tool for the problem.
Conclusion
Inheritance is a core principle of Object-Oriented Programming. It allows child classes to reuse and extend the properties and methods of parent classes. This helps reduce duplication, organize related classes, and create cleaner software structures.
However, inheritance should be used carefully. It works best when there is a clear is-a relationship between classes. When used correctly, inheritance can make code easier to maintain, easier to extend, and easier to understand.
For beginners, inheritance is an essential step toward understanding more advanced OOP topics such as polymorphism, abstraction, interfaces, and design patterns. Mastering inheritance provides a strong foundation for writing professional object-oriented code.

