__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> Inheritance / Single inheritance
--> Multiple inheritance
--> multi-level inheritance
--> Hierarchial inheritance
--> Hybrid inheritance
--> issubclass
--> isinstance
"""
""" Inheritance / Single inheritance
Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
Instead of starting from a scratch, you can create a class by deriving it from a pre-existing class
by writing the parent class name in parentheses after the new_class_name.
Single Inheritance Syntax:
Derived class is declared much like it's parent class.
class ChildClassName(ParentClass):
'Optional class documentation string'
class_suite (class variables , methods)
The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class.
A child class can also override data members and methods from the parent.
"""
class Base():
pass # Empty Class
class Derived(Base):
pass # Empty Class
"""
Single Inheritance
When a child class inherits only a single parent class."""
class Bhaskarao: # define parent class
parentAttr = 100 # class variable/local variable/ attribute
def __init__(self):
print ("Calling parent constructor")
def parentMethod(self):
print ('Calling parent method')
def setAttr_Name(self, attr):
Bhaskarao.parentAttr = attr # class_name.class_variable
def getAttr_Name(self):
print ("Parent attribute value:", Bhaskarao.parentAttr)
def python(self):
print("venkat")
print("priyanka")
print("subhashini")
print("sujana")
print("rindha")
# priyanka = Bhaskarao() # obj/instance creation
# print("class attribute value:",priyanka.parentAttr)
# priyanka.parentMethod()
# priyanka.setAttr_Name(444)
# priyanka.getAttr_Name()
# priyanka.python()
# single Inheritance
class Narendra(Bhaskarao): # define derived class / child class
def __init__(self):
print ("Calling child constructor")
def Narendra_Method(self):
print ('Calling child method')
# child_obj = Narendra() # instance of child , immediately child's init method will be executed
# child_obj.Narendra_Method()
# child_obj.python() # calls parent's method
# child_obj.parentMethod() # calls parent's method
# child_obj.setAttr_Name(200) # again call parent's method
# child_obj.getAttr_Name() # again call parent's method
"""
When the above code is executed, it produces the following result −
Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200
"""
"""
############# Multiple inheritance #############
When a child class inherits from more than one parent class
or
# In a similar way, you can drive a class from multiple parent classes as follows −
class A: # define your class A
.....
class B: # define your calss B
.....
class C(A, B): # subclass of A and B
.....
"""
class Chocolate:
def __init__(self):
print("calling chocolate method")
def ingedreint(self):
print("badam, pista and cashew are presented in this ")
def flavour(self):
print("choclate flavour")
class FiveStar:
def __init__(self):
print("calling five star method")
def taste(self):
print("completely filled with chocolate and caramel")
class Abcd:
def xyz(self):
print("called xyz method")
class DairyMilk(Chocolate, FiveStar, Abcd): # Multiple inheritance
Mahalakshmi = 444 # class attribute / class variable
def __init__(self):
print("calling Dairy Milk method")
def nutrition_facts(self):
print("energy contains: 4% \nproteins contain: 0% \nfat contains: 10% \ncarbohydrates contains: 5%")
# child_obj = DairyMilk()
# print(child_obj.Mahalakshmi)
# child_obj.nutrition_facts()
# child_obj.flavour()
# child_obj.taste()
# child_obj.ingedreint()
# child_obj.xyz()
""" ############# multi-level inheritance #############
When a child class becomes a parent class for another child class """
class subhashini(): # In general we called this(subhashini) as Base class
def abc(self):
print("called abc method available in subhashini class")
class venkat(subhashini): # In general we called this(venkat) as Derived class
def xyz(self):
print("called xyz method available in venkat class")
class ganesh(venkat): # In general we called this(ganesh) as Derived class
def ijk(self):
print("called ijk method available in ganesh class")
class rindha(ganesh):
def qwerty(self):
print("called qwerty method available in rindha class")
child_obj = rindha()
child_obj.xyz()
child_obj.abc()
child_obj.qwerty()
# print(issubclass(rindha, ganesh)) # True
# print(issubclass(rindha, venkat))
# print(issubclass(ganesh, rindha)) # False
# print(issubclass(venkat, rindha))
"""
How to check if a class is subclass of another?
Python provides a function issubclass() that directly tells us if a class is subclass of another class
You can use issubclass() or isinstance() functions to check a relationships of two classes and instances.
The issubclass(sub, sup) boolean function returns True, if the given subclass sub is indeed a subclass of the superclass sup.
The isinstance(obj, Class) boolean function returns True, if obj is an instance of class Class or is an instance of a subclass of Class
"""
# print(isinstance(child_obj, rindha))
# print(isinstance(child_obj, ganesh))
# print(isinstance(child_obj, venkat))
# print(isinstance(child_obj, subhashini))
""" ############# Hierarchial inheritance #############
single class inherited by multiple classes
or
When more than one derived classes are created from a single base,
this type of inheritance is called hierarchical inheritance. """
class FiveStar:
cost = 30 # class_variable / attribute
def __init__(self):
print("calling five star method")
def qwerty(self):
print("called qwerty method available in rindha class")
def taste(self):
print("completely filled with chocolate and caramel")
class Narendra (FiveStar):
pass
class Varun(FiveStar):
pass
class Manikanta(FiveStar):
pass
"""Hybrid Inheritance
Hybrid inheritance is done when we have to mix different types of inheritance within a single program,
for example, mixing single inheritance with multiple inheritance or multiple inheritance within a single program
"""
No comments:
Post a Comment