0% found this document useful (0 votes)
3 views

Inheritance (1)

Uploaded by

sakshamdubey022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Inheritance (1)

Uploaded by

sakshamdubey022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Inheritance-

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to


inherit attributes and methods from another class. This promotes code reusability and establishes a
hierarchical relationship between classes.

Syntax and Example-


In Python, inheritance is implemented by defining a new class that derives from an existing class. The
derived class (child class) inherits the attributes and methods of the base class (parent class).
# Parent class
class Person:
def __init__(self, name, id):
self.name = name
self.id = id

def display(self):
print(self.name, self.id)

# Child class
class Employee(Person):
def print_emp(self):
print("Employee class called")

# Creating an instance of the Employee class


emp = Employee("John", 101)
emp.display() # Output: John 101
emp.print_emp() # Output: Employee class called

Types of Inheritance
Python supports various types of inheritance:

1. Single Inheritance: Single inheritance is a fundamental concept in object-oriented


programming where a child class inherits properties and methods from a single parent class.

Example of Single Inheritance

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

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

# Child class inheriting from Person


class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age) # Call the constructor of the parent class
self.student_id = student_id

def display_student(self):
print(f"Student ID: {self.student_id}")

# Creating an instance of the Student class


student = Student("Alice", 20, "S12345")
student.display() # Calling the method from the parent class
student.display_student() # Calling the method from the child class

2. Multiple Inheritance: Multiple inheritance in Python allows a class to inherit attributes and
methods from more than one parent class.

Example of multiple Inheritance -


class Parent1:
def display1(self):
print("In class Parent1")

class Parent2:
def display2(self):
print("In class Parent2")

class Child(Parent1, Parent2):


pass
obj = Child()
obj.display1()
obj.display2()
3. Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits from
another parent class.

Example of Multilevel Inheritance-


# Base class
class Animal:
def eat(self):
print("Eating...")

# Derived class 1
class Mammal(Animal):
def walk(self):
print("Walking...")

# Derived class 2
class Dog(Mammal):
def bark(self):
print("Barking...")

# Creating an object of the Dog class


dog = Dog()
dog.eat() # Output: Eating...
dog.walk() # Output: Walking...
dog.bark() # Output: Barking...

4. Hierarchical Inheritance: Multiple child classes inherit from the same parent class.

Example of Hierarchical Inheritance-

# Parent class
class Animal:
def speak(self):
print("Animal speaks")

# Child class 1
class Dog(Animal):
def bark(self):
print("Dog barks")
# Child class 2
class Cat(Animal):
def meow(self):
print("Cat meows")

# Creating objects of child classes


dog = Dog()
cat = Cat()

# Accessing methods from the parent class


dog.speak() # Output: Animal speaks
dog.bark() # Output: Dog barks

cat.speak() # Output: Animal speaks


cat.meow() # Output: Cat meows

5. Hybrid Inheritance: A combination of two or more types of inheritance.

Example of Hierarchical Inheritance-


class Animal:
def speak(self):
print("Animal speaks")

class Mammal(Animal):
def give_birth(self):
print("Mammal gives birth")

class Bird(Animal):
def lay_eggs(self):
print("Bird lays eggs")

class Platypus(Mammal, Bird):


pass
platypus = Platypus()
platypus.speak() # Method from Animal class
platypus.give_birth() # Method from Mammal class
platypus.lay_eggs() # Method from Bird class

You might also like