What is Inheritance? What are the different forms of Inheritance? Write suitable codes to illustrate them.


Q.) What is Inheritance? What are the different forms of Inheritance? Write suitable codes to illustrate them.

Subject: Object Oriented Programming and Methodology

Inheritance in Programming:

Inheritance is a powerful feature in object-oriented programming (OOP) that allows a new class to be created based on an existing class. This new class inherits all the properties and methods of the parent class, which makes it easier to create and maintain related classes. Inheritance promotes code reusability, reduces redundancy, and enhances the maintainability and extensibility of software applications.

There are three main types of inheritance in most object-oriented programming languages:

  1. Single Inheritance: In single inheritance, a derived class (or child class) inherits properties and methods from a single parent class (or base class). The child class becomes a specialized version of the parent class and can add its own unique attributes and behaviors.

Syntax (in C++):

class ChildClass: public ParentClass {
// Child class body
};

Example:

class Animal {
public:
    std::string name;
    void eat();
};

class Dog: public Animal {
public:
    void bark();
};

int main() {
    Dog dog1;
    dog1.name = "Rex";
    dog1.eat();
    dog1.bark();
    return 0;
}
  1. Multiple Inheritance: In multiple inheritance, a derived class inherits properties and methods from multiple parent classes. This allows the derived class to combine features from different parent classes, resulting in a more flexible and powerful class.

Syntax (in C++):

class ChildClass: public ParentClass1, public ParentClass2 {
// Child class body
};

Example:

class Shape {
public:
    std::string color;
};

class Square: public Shape {
public:
    int length;
};

class Rectangle: public Shape {
public:
    int width, height;
};

class SquareRect: public Square, public Rectangle {
public:
    void displayArea();
};

int main() {
    SquareRect sqrRect;
    sqrRect.color = "red";
    sqrRect.length = 5;
    sqrRect.width = 4;
    sqrRect.height = 6;
    sqrRect.displayArea();
    return 0;
}
  1. Hierarchical Inheritance: Hierarchical inheritance, also known as a tree structure, occurs when more than one derived class inherits properties and methods from a single parent class. This creates a hierarchy of classes where the parent class is at the top and the derived classes are its children.

Syntax (in Java):

class ParentClass {
// Parent class body
}

class ChildClass1 extends ParentClass {
// Child class 1 body
}

class ChildClass2 extends ParentClass {
// Child class 2 body
}

Example:

class Animal {
public:
    std::string name;
    void eat();
};

class Dog: public Animal {
public:
    void bark();
};

class Cat: public Animal {
public:
    void meow();
};

int main() {
    Dog dog1;
    dog1.name = "Rex";
    dog1.eat();
    dog1.bark();

    Cat cat1;
    cat1.name = "Fluffy";
    cat1.eat();
    cat1.meow();
    return 0;
}

In conclusion, inheritance is a fundamental concept in OOP that enhances code reusability, reduces redundancy, and enables the creation of more flexible and powerful classes. Single inheritance allows a child class to inherit from a single parent class, multiple inheritance allows a child class to inherit from multiple parent classes, and hierarchical inheritance creates a hierarchy of classes where derived classes inherit from a single parent class.