Lecture 9 – Inheritance in Practice

Inheritance in Practice

Dive into Inheritance in Practice with real examples of method overriding, constructor chaining, and base class access. Learn how to use the super keyword and understand how constructors behave in inherited classes in both C++ and Java.

Introduction

In the previous lecture, you learned the concept and syntax of inheritance how one class can reuse and extend another.
Now it’s time to see how inheritance works in practice, especially when methods and constructors come into play.

Method Overriding

Method overriding allows a derived (child) class to provide a new version of a method that already exists in the base (parent) class.
This helps create behavior that fits the child class better.

Definition:

Overriding means redefining a method in the child class that already exists in the parent class with the same name, parameters, and return type.

Method Overriding

Lecture 8 – Inheritance: Concept and Syntax

Example (C++):

#include <iostream>
using namespace std;

class Animal {
public:
    void sound() {
        cout << "Animal makes a sound." << endl;
    }
};

class Dog : public Animal {
public:
    void sound() {
        cout << "Dog barks." << endl;
    }
};

int main() {
    Dog d;
    d.sound(); // Calls Dog’s version, not Animal’s
}

Explanation:

  • Both Animal and Dog have a sound() method.
  • The one in Dog overrides the parent version.

Example (Java):

class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound(); // Dog barks
    }
}

The @Override annotation in Java helps ensure the method truly overrides one from the parent class.

Using the super Keyword / Base Class Access

Sometimes we still want to call the parent class method even after overriding it.
We can do this using:

  • super in Java
  • Scope resolution :: in C++

Example (C++):

#include <iostream>
using namespace std;

class Animal {
public:
    void sound() {
        cout << "Animal makes a sound." << endl;
    }
};

class Dog : public Animal {
public:
    void sound() {
        cout << "Dog barks." << endl;
        Animal::sound(); // Access base class method
    }
};

int main() {
    Dog d;
    d.sound();
}

Output:

Dog barks.
Animal makes a sound.

Example (Java):

class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks.");
        super.sound(); // Call parent class method
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();
    }
}

The super keyword in Java refers to the parent class, allowing access to its methods or constructors.

Constructors in Inheritance

When an object of a derived class is created:

  1. The base class constructor runs first.
  2. Then the derived class constructor runs.

This ensures that the base part of the object is initialized before the child’s part.

Example (C++):

#include <iostream>
using namespace std;

class Animal {
public:
    Animal() {
        cout << "Animal constructor called." << endl;
    }
};

class Dog : public Animal {
public:
    Dog() {
        cout << "Dog constructor called." << endl;
    }
};

int main() {
    Dog d;
}

Output:

Animal constructor called.
Dog constructor called.

Example (Java):

class Animal {
    Animal() {
        System.out.println("Animal constructor called.");
    }
}

class Dog extends Animal {
    Dog() {
        System.out.println("Dog constructor called.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}

Same behavior:
Parent constructor executes before the child’s.

Parameterized Constructors in Inheritance

If the base class has a parameterized constructor, we can call it from the derived class using:

  • C++: Initialization list
  • Java: super() call

Example (C++):

#include <iostream>
using namespace std;

class Animal {
public:
    Animal(string type) {
        cout << "Animal type: " << type << endl;
    }
};

class Dog : public Animal {
public:
    Dog(string name) : Animal("Dog") { // Call parent constructor
        cout << "Dog name: " << name << endl;
    }
};

int main() {
    Dog d("Buddy");
}

Output:

Animal type: Dog
Dog name: Buddy

Example (Java):

class Animal {
    Animal(String type) {
        System.out.println("Animal type: " + type);
    }
}

class Dog extends Animal {
    Dog(String name) {
        super("Dog"); // Call parent constructor
        System.out.println("Dog name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog("Buddy");
    }
}

Output:

Animal type: Dog
Dog name: Buddy

Key Points to Remember

ConceptDescriptionExample
Method OverridingChild redefines parent’s methodvoid sound()
Base Class AccessCall parent’s versionsuper.sound() / Animal::sound()
ConstructorsParent constructor runs firstBase → Derived
Parameterized ConstructorsPass arguments to parentsuper("Dog")

Positive Thought for Students

“Inherit knowledge like code learn from what came before, but always add your own logic to make it better.”

Leave a Reply

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