OOP UNIT 4 Inheritance 1
OOP UNIT 4 Inheritance 1
(23EN1202)
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called
derived class.
class DerivedClass(BaseClass):
Body of derived class
Derived class inherits features from the base class, adding new features
to it. This results into re-usability of code.
def role(self):
class Employee(Person):
def role(self):
class Customer(Person):
def role(self):
class FamilyMember(Person):
def role(self):
def display_role(person):
return person.role()
employee = Employee()
customer = Customer()
family_member = FamilyMember()
dog = Dog()
print(isinstance(dog, Dog)) # Output: True
print(isinstance(dog, object)) # Output: True (all objects are
instances of the base object class)
issubclass()
⮚ Determines if a class is a subclass of another class.
⮚ Returns True if the class is a subclass of the specified class or any of its
subclasses. False otherwise.
Example:
class Animal:
pass
class Dog(Animal):
pass
class Dog(Animal):
pass
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # Call the constructor of the Parent class
self.age = age
class Child(Parent):
def greet(self):
return super().greet() + ", and Hello from Child"
child = Child()
print(child.greet()) # Output: Hello from Parent, and Hello from Child
Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Multi-level Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Inheritance
⮚In single inheritance, a subclass inherits from only one
superclass.
⮚This is the simplest form of inheritance.
Example:
class Animal:
def __init__(self, species):
self.species = species
class Mammal(Animal):
def __init__(self, species, has_hair):
super().__init__(species)
self.has_hair = has_hair
class MultimediaDevice:
def play_video(self):
print("Playing a video...")
smartphone = Smartphone()
smartphone.make_call() # Output: Making a call...
smartphone.play_video() # Output: Playing a video...
Multi-level Inheritance
⮚ In multilevel inheritance, a subclass inherits from another subclass,
forming a chain of inheritance.
⮚ This creates a hierarchy of classes where each class inherits from the
class above it.
Example:
class Intern:
def __init__(self, name):
self.name = name
class RegularEmployee(Intern):
def __init__(self, name, department):
super().__init__(name)
self.department = department
class Manager(RegularEmployee):
def __init__(self, name, department, team):
super().__init__(name, department)
self.team = team
Example
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)
The __init__() Function
The examples above are classes and objects in their simplest form,
in real life applications.
To understand the meaning of classes we have to understand the
built-in __init__() function.
All classes have a function called __init__(), which is always
executed when the class is being initiated.
Use the __init__() function to assign values to object properties, or
other operations that are necessary to do when the object is being
created:
Example
Create a class named Person, use the __init__() function to assign values for name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Output: John
36
The __str__() function controls what should be returned when the class
object is represented as a string.
Example
class Person:
pass
THANK YOU