Inheritance: Concept and Syntax
Learn the concept and syntax of Inheritance in Object-Oriented Programming. Understand the need for inheritance, how base and derived classes work, and how the “is-a” relationship improves code reusability using practical C++ and Java examples.
What is Inheritance?
Inheritance is one of the four core pillars of Object-Oriented Programming (OOP).
It allows one class (child) to reuse and extend the properties and behaviors of another class (parent).
Definition:
Inheritance is a mechanism where one class (called derived or subclass) acquires the properties and methods of another class (called base or superclass).

Lecture 7 – Arrays and Objects
Why Do We Need Inheritance?
Inheritance helps in:
- Code Reusability – You can use existing code instead of rewriting it.
- Extensibility – You can add new features to existing classes easily.
- Organization – Helps maintain clean and structured code.
- Relationship Modeling – Models real-world hierarchies (e.g., Animal → Dog).

Base and Derived Classes
When using inheritance:
- The Base Class (Parent/Superclass) defines general properties and behaviors.
- The Derived Class (Child/Subclass) inherits from it and can add or override functionality.
Example (C++):
#include <iostream>
using namespace std;
// Base Class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived Class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
}
Explanation:
Doginherits fromAnimalusing thepublickeyword.Dogcan now use botheat()andbark().
Example (Java):
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited
d.bark(); // Defined in Dog
}
}
In Java, the keyword extends is used instead of :.
“is-a” Relationship
Inheritance represents an “is-a” relationship between classes.
For example:
- A Dog is an Animal
- A Car is a Vehicle
- A Student is a Person
This helps us create logical hierarchies in our code.
Example (Hierarchy Diagram):
Animal
/ \
Dog Cat
Both Dog and Cat inherit from Animal, but can have their own unique behaviors too.
Code Reusability and Extension
You can extend a base class to add new features.
Example (C++):
class Vehicle {
public:
void start() {
cout << "Vehicle started." << endl;
}
};
class Car : public Vehicle {
public:
void playMusic() {
cout << "Playing music in the car." << endl;
}
};
Here, Car automatically gets start() from Vehicle without rewriting it.
Access Specifiers in Inheritance (C++)
When inheriting, you must specify the access level:
| Type | Description | Example |
|---|---|---|
public | Public members stay public | class Dog : public Animal |
protected | Public → protected | class Dog : protected Animal |
private | All become private | class Dog : private Animal |
In Java, inheritance is always public (by default) unless restricted using keywords like private or final.
Syntax Overview
| Language | Syntax |
|---|---|
| C++ | class Derived : public Base { ... }; |
| Java | class Derived extends Base { ... } |
Summary of Lecture 8
| Concept | Meaning | Example |
|---|---|---|
| Inheritance | One class uses another’s properties | class Dog extends Animal |
| Base Class | Parent with general features | Animal |
| Derived Class | Child that inherits | Dog |
| “is-a” Relationship | Logical connection | Dog is an Animal |
| Reusability | Avoid code repetition | Car inherits Vehicle |
Positive Thought for Students
“Inheritance in coding like in life is not about copying, but about carrying forward the best parts and adding your own.”




