0% found this document useful (0 votes)
6 views7 pages

Understanding Inheritance in Python

Inheritance in OOP allows a child class to inherit properties and methods from a parent class, promoting code reuse and a hierarchical structure. There are various types of inheritance in Python, including multilevel and multiple inheritance, which enable different relationships between classes. Additionally, concepts like method overloading, method overriding, and function decorators enhance the functionality and behavior of classes.

Uploaded by

Jksshab
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 views7 pages

Understanding Inheritance in Python

Inheritance in OOP allows a child class to inherit properties and methods from a parent class, promoting code reuse and a hierarchical structure. There are various types of inheritance in Python, including multilevel and multiple inheritance, which enable different relationships between classes. Additionally, concepts like method overloading, method overriding, and function decorators enhance the functionality and behavior of classes.

Uploaded by

Jksshab
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

What is Inheritance?

Inheritance is a mechanism in OOP that lets you create a new class (child class) that inherits
properties and methods from an existing class (parent class). This allows for code reuse and
promotes a hierarchical relationship between classes.

Types of Inheritance
Inheritance in Python comes in various forms, each with its own purpose and characteristics.

Multilevel Inheritance Multiple Inheritance

A child class inherits from a parent class, A child class inherits from multiple parent
which itself inherits from another parent classes.
class, forming a chain of inheritance.
Multiple Inheritance
Deriving a class from more than one base class is known as Multiple Inheritance

class Engine:
Parent Class 1 def start(self):
print("Engine starts“)
Provides its attributes and methods.
class Wheels:
def rotate(self):
print("Wheels are rotating“)
Parent Class 2
class Car(Engine, Wheels):
Contributes its own attributes and methods. def drive(self):
print("Car is driving“)

my_car = Car()
Child Class
my_car.start() # Inherited from Engine
Inherits from both parent classes. my_car.rotate() # Inherited from Wheels
my_car.drive() # Defined in Car
Multilevel Inheritance
Deriving a class from another derived class is known as Multilevel Inheritance.

class Animal:
Grandparent Class def speak(self):
print(“Animal makes a sound“)
The foundation of inheritance.
class Mammal(Animal):
def walk(self):
print("Mammal walks on four legs“)
Parent Class
class Dog(Mammal):
Inherits from the grandparent class. def bark(self):
print("Dog barks“)

dog = Dog()
Child Class
[Link]()
Inherits from the parent class. [Link]()
[Link]()
Method Overloading
• Method overloading is a feature that allows you to define multiple methods in a class with
the same name, but different parameters.
• The compiler determines which method to call based on the number of arguments provided.

class MathOperations:
def add(self, a, b, c=0):
return a + b + c

math = MathOperations()
print([Link](2, 3))
print([Link](2, 3, 4))
Method Overriding
• Method overriding is a technique where a subclass provides a specific implementation of a method tha
defined in its parent class.
Example:

class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def speak(self):
print("Dog barks")

animal = Animal()
dog = Dog()
[Link]()
[Link]()
Function Decorators
Decorators are powerful tools in Python for modifying the behavior of method in a class.

• @staticmethod
• Transforms a method into a static method
• A static method does not receive an implicit first argument.
• @classmethod
• Transforms a method into a class method.
• A class method receives the class as implicit first argument, just like an instance method receives the in
• @property
• A property object has getter, setter and deleter methods which can be used as decorators.
• These method create a copy of the property with the corresponding accessor function.
• getter method is used for getting attributes value.
• setter method is used for setting attributes value.
• deleter method is used for deleting attributes value.
Function Decorators(Example)
class Numbers: @[Link]
multiplier = int(input("Enter a def value(self, xy_tuple):
number: ")) self.x , self.y = xy_tuple
def __init__(self, x, y): self.x *= 100
self.x = x self.y *= 100
self.y = y
@[Link]
def add(self): def value(self):
return self.x + self.y print("Deleting Values")
del self.x
@classmethod del self.y
def multiply(self, a):
return a * [Link] n = Numbers(10, 20)
print("Sum = ", [Link]())
@staticmethod print("Product = ",
def subtract(b, c): [Link](10))
return b-c print("Difference = ",
[Link](5, 3))
@property a, b = [Link]
def value(self): print(a, b)
pass [Link] = 8, 5
a, b = [Link]
@[Link] print(a, b)
def value(self): del [Link]
return (self.x, self.y)

You might also like