Inheritance

 

Inheritance in Object-Oriented Programming (OOP) - Complete Explanation

What is Inheritance?

Inheritance is a key principle of Object-Oriented Programming (OOP) that allows one class to inherit properties (attributes) and behaviors (methods) from another class. It establishes a relationship between parent (superclass/base class) and child (subclass/derived class) classes, enabling code reusability, hierarchy, and modular design.


1. Key Features of Inheritance

  1. Code Reusability: Avoids code duplication by allowing a subclass to reuse attributes and methods from its superclass.
  2. Extensibility: A child class can extend the functionality of the parent class by adding new methods or overriding existing ones.
  3. Hierarchy: Models real-world relationships by defining a structured class hierarchy.
  4. Method Overriding: The child class can redefine the parent’s method to provide a specific implementation.
  5. Access Control: Controls which attributes and methods are inherited using access modifiers (privateprotectedpublic).

2. Types of Inheritance

(A) Single Inheritance

A subclass inherits from only one superclass.

Example (Python)

python

class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Bark" dog = Dog() print(dog.speak()) # Output: Bark

(B) Multilevel Inheritance

A class inherits from another class, which itself is inherited by another class (forming a chain).

Example (Java)

java

class Animal { void eat() { System.out.println("This animal eats food."); } } class Mammal extends Animal { void breathe() { System.out.println("Mammals breathe air."); } } class Dog extends Mammal { void bark() { System.out.println("Dog barks."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited from Animal dog.breathe(); // Inherited from Mammal dog.bark(); // Defined in Dog } }

(C) Multiple Inheritance (Supported in Python, Not in Java)

A subclass inherits from multiple parent classes. Java does not support multiple inheritance with classes but allows it with interfaces.

Example (Python)

python

class Animal: def eat(self): return "Eating" class Canine: def howl(self): return "Howling" class Dog(Animal, Canine): # Multiple Inheritance def bark(self): return "Barking" dog = Dog() print(dog.eat()) # Inherited from Animal print(dog.howl()) # Inherited from Canine print(dog.bark()) # Defined in Dog

(D) Hierarchical Inheritance

Multiple subclasses inherit from a single superclass.

Example (Java)

java

class Animal { void sound() { System.out.println("Some generic animal sound"); } } class Dog extends Animal { void bark() { System.out.println("Dog barks."); } } class Cat extends Animal { void meow() { System.out.println("Cat meows."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); dog.sound(); // Inherited from Animal dog.bark(); // Defined in Dog cat.sound(); // Inherited from Animal cat.meow(); // Defined in Cat } }



4. Access Modifiers and Inheritance

ModifierSame ClassSame PackageSubclassOutside Package
public
protected
default (no modifier)
private

5. super Keyword in Inheritance

  • super is used to refer to the parent class's methods and constructors.
  • It is often used when a subclass overrides a method but still needs the parent class’s functionality.

Example (Java)

java

class Parent { Parent() { System.out.println("Parent constructor"); } void show() { System.out.println("Parent class method"); } } class Child extends Parent { Child() { super(); // Calls Parent constructor System.out.println("Child constructor"); } void show() { super.show(); // Calls Parent's show() method System.out.println("Child class method"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); } }

Output:

kotlin
Parent constructor Child constructor Parent class method Child class method

6. Abstract Classes and Inheritance

An abstract class cannot be instantiated but can be inherited. It may contain abstract methods (without implementation).

Example (Java)

java

abstract class Animal { abstract void sound(); // Abstract method } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); // Output: Dog barks } }

7. Interfaces and Inheritance (Java-Specific)

Java supports multiple inheritance through interfaces.

Example

java

interface Animal { void eat(); } interface Canine { void howl(); } class Dog implements Animal, Canine { public void eat() { System.out.println("Dog eats food."); } public void howl() { System.out.println("Dog howls."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); dog.howl(); } }

8. Real-World Analogy of Inheritance

Imagine a Vehicle class that has properties like speed and fuelCapacity. Now, we create subclasses:

  • Car extends Vehicle → Inherits speed and fuelCapacity, adds numDoors.
  • Bike extends Vehicle → Inherits speed and fuelCapacity, adds hasCarrier.

This demonstrates real-world hierarchical relationships.


Conclusion

  • Inheritance allows a child class to reuse and extend the parent class.
  • Different types: Single, Multilevel, Multiple, Hierarchical, Hybrid.
  • Supports method overridingsuper keyword, abstract classes, and interfaces.
  • Encourages code reusability and modular programming.

Comments

Popular posts from this blog

My Placement Preparation