INHERITANCE
INHERITANCE
By Riya Shikha
what is inheritance
inheritance is a fundamental concept in object-oriented
programming (OOP) where a class (called a child or
subclass) derives properties and behaviors (methods
and attributes) from another class (called a parent or
superclas
TYPES OF
INHERITANCE
1) SINGLE
TYPES OF INHERITANCE
2) MUTIPLE
INHERITANCE INHERITANCE
3) MULTILEVEL
INHERITANCE
4) HIERARCHICAL
INHERITANCE
5) HYBRID
INHERITANCE
SINGLE INHERITANCE
1. Single Inheritance
A child class inherits from one parent class.
class Parent:
def show(self):
print("Parent class")
class Child(Parent):
pass
c = Child()
c.show() # Output: Parent class
2) MULTIPLE INHERITANCE 2. Multiple Inheritance
A child class inherits from more than one parent class.
class Father:
def skill(self):
print("Driving")
class Mother:
def skill(self):
print("Cooking")
c = Child()
c.skill() # Output: Driving (depends on method resolution order)
3) MULTILEVEL INHERITANCE
A class is derived from a class that is also derived from another class.
python
class Grandparent:
def speak(self):
print("Grandparent")
class Parent(Grandparent):
passclass Child(Parent):
pass
c = Child()
c.speak() # Output: Grandparent
4. Hierarchical Inheritance
Multiple child classes inherit from the same parent
class.
class Parent:
def show(self):
print("Parent")
class Child1(Parent):
pass
class Child2(Parent):
pass
c1 = Child1()
c1.show() # Output: Parent
5. Hybrid Inheritance
A combination of two or more types of inheritance (like
multiple + multilevel).
class Person:
def info(self):
print("I am a person")
# Intermediate class 2
class Student(Person):
def student_info(self):
print("I am a student")
# Creating object
intern = Intern()
intern.info() # From Person
intern.employee_info() # From Employee
intern.student_info() # From Student
intern.intern_info() # From Intern
THANK YOU