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

Inheritance'

The document explains the Python super() function, which allows a child class to access methods from its parent class, facilitating code reusability and supporting both single and multiple inheritances. It also covers the issubclass() function to check subclass relationships, method overriding for implementing specific behaviors in subclasses, and the Method Resolution Order (MRO) for determining the order of method searches in inheritance. Examples are provided to illustrate each concept effectively.

Uploaded by

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

Inheritance'

The document explains the Python super() function, which allows a child class to access methods from its parent class, facilitating code reusability and supporting both single and multiple inheritances. It also covers the issubclass() function to check subclass relationships, method overriding for implementing specific behaviors in subclasses, and the Method Resolution Order (MRO) for determining the order of method searches in inheritance. Examples are provided to illustrate each concept effectively.

Uploaded by

rubyjeyamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Python super() function:

• When a class inherits all properties and behavior from the


parent class is called inheritance. In such a case, the inherited
class is a subclass and the latter class is the parent class.

• In child class, we can refer to parent class by using the


super() function. The super function returns a temporary
object of the parent class that allows us to call a parent class
method inside a child class method.

Benefits of using the super() function:

1. We are not required to remember or specify the parent class


name to access its methods.
2. We can use the super() function in both single and multiple
inheritances.
3. The super() function support code reusability as there is no
need to write the entire function
Example:
class Company:
def company_name(self):
return 'Google‘
class Employee(Company):
def info(self):
# Calling the superclass method using
super()function
c_name = super().company_name()
print("Jessa works at", c_name)

# Creating object of child class


emp = Employee()
emp.info()

OUTPUT:
Jessa works at Google
issubclass() :

• In Python, we can verify whether a particular class is a


subclass of another class. For this purpose, we can use
Python built-in function issubclass(). This function returns
True if the given class is the subclass of the specified
class. Otherwise, it returns False.

Syntax:
issubclass(class, classinfo)
– Where,
– class: class to be checked.
– classinfo: a class, type, or a tuple of classes or data types.

Return Value from issubclass()


issubclass() returns:

True: if class is subclass of a class, or any element of the tuple


False: otherwise
Example:
class Company:
def fun1(self):
print("Inside parent class")
class Employee(Company):
def fun2(self):
print("Inside child class.")
class Player:
def fun3(self):
print("Inside Player class.")
# Result True
print(issubclass(Employee, Company))
# Result False
print(issubclass(Employee, list))
# Result False
print(issubclass(Player, Company))
# Result True
print(issubclass(Employee, (list, Company)))
# Result True
Method Overriding:
Method overriding is an ability of any object-oriented
programming language that allows a subclass or child class to
provide a specific implementation of a method that is already
provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, the same
parameters or signature, and same return type(or sub-type) as
a method in its super-class, then the method in the subclass is
said to override the method in the super-class.
Example:
# Python program to demonstrate # Defining parent class
class Parent():
# Constructor
def __init__(self):
self.value = "Inside Parent"
# Parent's show method
def show(self):
(self.value) # Defining child class
class Child(Parent):
# Constructor
def __init__(self):
super().__init__() # Call parent constructor
self.value = "Inside Child"

# Child's show method


def show(self):
(self.value)
obj1 = Parent()
obj2 = Child()
obj1.show()
# Should print "Inside Parent"

obj2.show()
# Should print "Inside Child"
Method Resolution Order in Python:

• In Python, Method Resolution Order(MRO) is the order by


which Python looks for a method or attribute. First, the
method or attribute is searched within a class, and then it
follows the order we specified while inheriting.

• In multiple inheritance, the following search order is followed.

• First, it searches in the current parent class if not available,


then searches in the parents class specified while inheriting
(that is left to right.)
• We can get the MRO of a class. For this purpose, we can use
either the mro attribute or the mro() method.*.
Example:
class A:
def process(self):
print(" In class A")
class B(A):
def process(self):
print(" In class B")
class C(B, A):
def process(self):
print(" In class C")

# Creating object of C class


C1 = C()
C1.process()
print(C.mro())
# In class C # [<class '__main__.C'>, <class '__main__.B'>, <class
'__main__.A'>, <class 'object'>]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy