Inheritance Class

Description

๐Ÿงฌ What is Inheritance?

Inheritance allows one class (child/derived) to acquire the properties and behaviors of another class (parent/base).
It promotes code reusability and hierarchical classification.

๐Ÿงช Example of Inheritance

class Animal:
    def speak(self):
        print("Animals make sounds")

class Dog(Animal):
    def speak(self):
        print("Dog barks")

class Cat(Animal):
    def speak(self):
        print("Cat meows")

# Testing
d = Dog()
c = Cat()
d.speak()  # Output: Dog barks
c.speak()  # Output: Cat meows

๐ŸŽฏ Why You Should Learn Inheritance

โœ”๏ธ Encourages code reuse (DRY principle)
โœ”๏ธ Helps manage large codebases
โœ”๏ธ Enhances program structure with logical hierarchies

๐Ÿ“‹ Use Cases of Inheritance

Scenario Description
Employee → Manager Manager inherits common attributes from Employee but adds specific roles
Vehicle → Car, Bike All vehicles have wheels and engine, subclasses add specific behavior
Shape → Circle, Rectangle Each shape shares basic geometric behaviors, but implements them differently

๐ŸŒ Real-time Scenarios

๐Ÿฅ In a hospital management system, base class Person can be extended by Patient and Doctor
๐Ÿ›๏ธ In an e-commerce site, base Product class can be extended by Electronics, Clothing, etc.
๐Ÿš— In a vehicle app, the base class Vehicle can be inherited by Car, Bus, Bike

๐Ÿงฉ Core Concepts in Inheritance

No. Concept Description
1๏ธโƒฃ Base Class (Parent) The class being inherited from.
2๏ธโƒฃ Derived Class (Child) The class that inherits from the parent.
3๏ธโƒฃ super() Function Calls methods of the parent class from the child class.
4๏ธโƒฃ Method Overriding Redefines a parent class method in the child class.
5๏ธโƒฃ Single Inheritance Child inherits from one parent.
6๏ธโƒฃ Multilevel Inheritance Inheritance across multiple levels.
7๏ธโƒฃ Multiple Inheritance Child class inherits from multiple parents.
8๏ธโƒฃ Hierarchical Inheritance Multiple child classes from one parent class.
9๏ธโƒฃ Hybrid Inheritance Mix of more than one type of inheritance.
๐Ÿ”Ÿ isinstance() Checks if an object is an instance of a class.
1๏ธโƒฃ1๏ธโƒฃ issubclass() Checks if a class is a subclass of another.

๐Ÿ“ Practice Tests

โœ… Create a base class called Appliance and extend it with WashingMachine and Refrigerator
โœ… Add a method in base class and override it in child classes
โœ… Demonstrate single, multilevel, and multiple inheritance

๐Ÿฅ Real-time Inheritance Example – Hospital System

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}")

class Doctor(Person):
    def __init__(self, name, age, specialization):
        super().__init__(name, age)
        self.specialization = specialization

    def display_info(self):
        super().display_info()
        print(f"Specialization: {self.specialization}")

class Patient(Person):
    def __init__(self, name, age, illness):
        super().__init__(name, age)
        self.illness = illness

    def display_info(self):
        super().display_info()
        print(f"Illness: {self.illness}")

# Usage
doc = Doctor("Dr. Ramesh", 45, "Cardiologist")
pat = Patient("Sita", 30, "Fever")

# Show details
doc.display_info()
print("-----")
pat.display_info()

๐Ÿ“˜ 1. Single Inheritance

One child class inherits from one parent class.
class Vehicle:
    def start_engine(self):
        print("Engine started")

class Car(Vehicle):
    def play_music(self):
        print("Playing music")

c = Car()
c.start_engine()
c.play_music()

๐Ÿ“˜ 2. Multilevel Inheritance

A class inherits from a derived class which inherits from another class.
class Person:
    def info(self):
        print("This is a person")

class Doctor(Person):
    def specialization(self):
        print("Specialization in medicine")

class Surgeon(Doctor):
    def surgery(self):
        print("Performs surgery")

s = Surgeon()
s.info()
s.specialization()
s.surgery()

๐Ÿ“˜ 3. Multiple Inheritance

One class inherits from multiple parent classes.
class Camera:
    def take_photo(self):
        print("Taking photo")

class Phone:
    def make_call(self):
        print("Calling...")

class Smartphone(Camera, Phone):
    def browse_internet(self):
        print("Browsing internet")

s = Smartphone()
s.take_photo()
s.make_call()
s.browse_internet()

โœ… Summary Table

๐Ÿท Type ๐Ÿ” Definition ๐Ÿ“Œ Real-time Example
Single One child class inherits from one parent Car inherits from Vehicle
Multilevel A class inherits from a derived class Surgeon → Doctor → Person
Multiple A class inherits from multiple parents Smartphone → Camera + Phone