Lecture 8 – Inheritance: Concept and Syntax

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).

What is Inheritance?

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).
Why Do We Need Inheritance?

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:

  • Dog inherits from Animal using the public keyword.
  • Dog can now use both eat() and bark().

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:

TypeDescriptionExample
publicPublic members stay publicclass Dog : public Animal
protectedPublic → protectedclass Dog : protected Animal
privateAll become privateclass Dog : private Animal

In Java, inheritance is always public (by default) unless restricted using keywords like private or final.

Syntax Overview

LanguageSyntax
C++class Derived : public Base { ... };
Javaclass Derived extends Base { ... }

Summary of Lecture 8

ConceptMeaningExample
InheritanceOne class uses another’s propertiesclass Dog extends Animal
Base ClassParent with general featuresAnimal
Derived ClassChild that inheritsDog
“is-a” RelationshipLogical connectionDog is an Animal
ReusabilityAvoid code repetitionCar 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.”

Leave a Reply

Your email address will not be published. Required fields are marked *