0% found this document useful (0 votes)
18 views3 pages

Python Inheritance Examples Explained

The document discusses different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate each type. Single inheritance allows a child class to inherit from one parent class. Multiple inheritance allows a child class to inherit from multiple parent classes. Multilevel inheritance allows a grandchild class to inherit from a parent and grandparent. Hierarchical inheritance involves multiple classes inheriting from a common parent class but not inheriting from each other. Hybrid inheritance combines characteristics of multiple and hierarchical inheritance where a class inherits from multiple parent classes that are themselves related through inheritance.

Uploaded by

Vaishu Gowda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Python Inheritance Examples Explained

The document discusses different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides code examples to demonstrate each type. Single inheritance allows a child class to inherit from one parent class. Multiple inheritance allows a child class to inherit from multiple parent classes. Multilevel inheritance allows a grandchild class to inherit from a parent and grandparent. Hierarchical inheritance involves multiple classes inheriting from a common parent class but not inheriting from each other. Hybrid inheritance combines characteristics of multiple and hierarchical inheritance where a class inherits from multiple parent classes that are themselves related through inheritance.

Uploaded by

Vaishu Gowda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#inheritance

class Person(object):
def __init__(self,name):
[Link]=name
def getName(self):
return [Link]
def isEmployee(self):
return False

class Employee(Person):
def isEmployee(self):
return True

emp = Person("Geek1")
print([Link](),[Link]())
emp = Employee("Geek2")
print([Link](),[Link]())

#single inheritance
class Parent:
def func1(self):
print("this function is in parent class")

class Child(Parent):
def func2(self):
print("this function is in child class")

object=Child()
object.func1()
object.func2()

#Multiple inheritance
class mother:
mothername=""

def mother(self):
print([Link])

class father:
fathername=""

def father(self):
print([Link])

class son(mother,father):
def parents(self):
print("father:",[Link])
print("mother:",[Link])

s1=son()
[Link]="RAMA"
[Link]="SITA"
[Link]()

#multilevel inheritance
class grandfather:
def __init__(self,grandfathername):
[Link]=grandfathername
class father(grandfather):
def __init__(self,fathername,grandfathername):
[Link]=fathername
grandfather.__init__(self,grandfathername)

class son(father):
def __init__(self,sonname,fathername,grandfathername):
[Link]=sonname
father.__init__(self,fathername,grandfathername)

def print_name(self):
print("grandfather name:",[Link])
print("father name",[Link])
print("son name",[Link])

s1=son("prince","rampal","lal mani")
print([Link])
s1.print_name()

#hierarchical inheritance
class parent:
def func1(self):
print("this function is a parent class")

class child1(parent):
def func2(self):
print("this function is in child1")

class child2(parent):
def func3(self):
print("this function is in child2")

object1=child1()
object2=child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()

#hybrid inheritance
class school:
def func1(self):
print("this function is in school")

class student1(school):
def func2(self):
print("this function is in student1")

class student2(school):
def func3(self):
print("this function is in student2")

class student3(school):
def func4(self):
print("this function is in student3")

object=student3()
object.func1()
object.func4()

Common questions

Powered by AI

The class structure in Source 1 illustrates several principles of object-oriented programming: inheritance, encapsulation, and polymorphism. Inheritance is shown through different types (single, multiple, multilevel, hierarchical, and hybrid), allowing classes to inherit properties and methods from other classes. Encapsulation is observed as each class encapsulates its methods and properties, such as the 'name' property in 'Person'. Polymorphism is evident in the 'isEmployee' method, which behaves differently in the 'Person' and 'Employee' classes .

Implementing a method in the 'son' class to print names from all hierarchical levels is a thoughtful design choice. It centralizes functionality related to hierarchy traversal, ensuring that name retrieval from all ancestors is accessible via a single method call. This approach enhances modularization, supports easy modification, and aligns with the principle of minimizing direct manipulation of data from outside the class .

Inheritance enhances code reuse by allowing new classes to incorporate properties and methods of existing classes, reducing redundancy. For instance, 'Employee' inherits from 'Person', utilizing its 'name' attribute and 'getName' method, thereby preventing repeated code. Moreover, it simplifies maintenance, where changes in the parent class propagate to children automatically, ensuring consistent behavior across elements .

Polymorphism is demonstrated through the 'isEmployee' method, which is defined in both 'Person' and 'Employee' classes. In 'Person', 'isEmployee' returns False, while in 'Employee', it returns True, showcasing method overriding. This way, different objects can interact through a common interface, yet respond differently depending on their specific class .

Constructors play a crucial role in multilevel inheritance by managing initialization of properties across different hierarchical levels. In the example, each constructor (__init__) initializes its unique properties ('grandfathername', 'fathername', 'sonname') and calls its superclass's constructor to ensure complete setup of inherited attributes. This ensures that all necessary data for each level of inheritance is appropriately initialized and maintained .

Single inheritance, illustrated by the classes 'Parent' and 'Child', involves a child class inheriting from one parent class, which allows the child to access methods of the parent, like 'func1'. Multiple inheritance, shown with the 'son', 'mother', and 'father' classes, allows a single child class ('son') to inherit from more than one parent class, enabling access to both parents' properties and methods, like 'fathername' and 'mothername' .

Multilevel inheritance is implemented where the 'son' class inherits from 'father', which in turn inherits from 'grandfather'. Each class initializes its respective name property ('sonname', 'fathername', 'grandfathername'), creating a chain of inheritance from 'grandfather' to 'father' to 'son'. This setup allows the 'son' class to access and print names associated with all levels of its hierarchy using the 'print_name' method .

Encapsulation is maintained by defining class variables and methods within each class's constructor and method definitions. For example, in the 'Person' and 'Employee' classes, data such as 'name' is encapsulated within the object instance. Methods like 'getName' and 'isEmployee' access these private attributes, maintaining separation between an object’s internal state and external interaction .

Instance variables are defined inside methods like '__init__', and are unique to each object instance, such as 'name' in 'Person' and 'Employee'. Class variables, although not explicitly defined in the provided code, would be shared across all instances of a class if defined outside of methods. The examples in the code focus on encapsulating data per individual instance .

Hierarchical inheritance allows multiple child classes to inherit from a single parent class, as seen with 'child1' and 'child2' inheriting from 'parent'. This structure supports reusability, where each child class can utilize the 'func1' method from 'parent', while maintaining their unique methods ('func2', 'func3'). It also organizes the code, highlighting commonality among children within a hierarchy .

You might also like