0% found this document useful (0 votes)
4 views19 pages

Understanding Inheritance in Python

This document covers Unit III of a Python programming course focusing on Object Oriented Programming concepts such as inheritance, encapsulation, and polymorphism. It explains various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid, along with method overriding and the use of super(). Additionally, it discusses real-world applications, best practices, and common pitfalls associated with inheritance in Python.

Uploaded by

shashisekar775
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)
4 views19 pages

Understanding Inheritance in Python

This document covers Unit III of a Python programming course focusing on Object Oriented Programming concepts such as inheritance, encapsulation, and polymorphism. It explains various types of inheritance including single, multiple, multilevel, hierarchical, and hybrid, along with method overriding and the use of super(). Additionally, it discusses real-world applications, best practices, and common pitfalls associated with inheritance in Python.

Uploaded by

shashisekar775
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

Programming with Python

Unit III
[Link]
Assistant Professor
Department of B Com(CA)
PSGR Krishnammal College for Women, Coimbatore
SYLLABUS-UNIT III

Object Oriented Programming: Classes and Objects –


Constructors – Destructors Getter and Setter Methods –
Encapsulations – Inheritance – Polymorphism – Abstract
Classes and Interfaces.
Inheritance in Python

• Exploring Code Reusability, Hierarchies, and


Best Practices
Learning Objectives
• Understand what inheritance is and its role in object-
oriented programming
• Learn how to implement inheritance in Python
• Explore types of inheritance: single, multiple, multilevel,
hierarchical, hybrid
• Understand method overriding and use of super()
• Review real-world applications and best practices
• Discuss common pitfalls and how to avoid them
What is Inheritance?
• Inheritance allows a class to acquire properties and behaviors of another
class

• Helps reuse code and avoid duplication

• Establishes relationships between classes (parent and child)

• Supports modular and scalable design

Example:

A car is a type of vehicle – it inherits common properties like speed and fuel
capacity.
Why Use Inheritance?
✔ Code reusability – common methods and attributes
defined once

✔ Simplified maintenance – updates in base class reflect in


subclasses

✔ Consistency – standardized interfaces for related objects

✔ Scalability – extend functionality without modifying


existing code
Basic Syntax of Inheritance
class Parent:
def greet(self):
print("Hello from Parent")

class Child(Parent):
pass

c = Child()
[Link]() # Accesses parent method
✔ Child class inherits from Parent and accesses its methods.
Single Inheritance
• One child class inherits from one parent class
• Simplest form of inheritance

Example:
class Animal:
def speak(self):
print("Animal sound")

class Dog(Animal):
def bark(self):
print("Woof!")

dog = Dog()
[Link]()
[Link]()
Method Overriding
• Child class can redefine methods from the parent
• Allows customization without changing base class

Example:
class Animal:
def speak(self):
print("Animal sound")

class Dog(Animal):
def speak(self):
print("Woof!")

dog = Dog()
[Link]()
✔ Overridden method in the child class takes precedence.
Using super()
• Call parent’s method to extend functionality

Example:
class Animal:
def speak(self):
print("Animal sound")

class Dog(Animal):
def speak(self):
super().speak()
print("Woof!")

dog = Dog()
[Link]()
✔ Combines behaviors from both parent and child.
Multiple Inheritance
• A child inherits from more than one parent
• Useful when combining functionalities

Example:
class Father:
def skills(self):
print("Gardening skills")

class Mother:
def talents(self):
print("Cooking talents")

class Child(Father, Mother):


def hobbies(self):
print("Playing sports")

c = Child()
[Link]()
[Link]()
[Link]()
Multilevel Inheritance
• A chain where a class inherits from a parent which itself inherits from another class

Example:
class Grandparent:
def heritage(self):
print("Grandparent heritage")

class Parent(Grandparent):
def traits(self):
print("Parent traits")

class Child(Parent):
def identity(self):
print("Child identity")
c = Child()
[Link]()
[Link]()
[Link]()
Hierarchical Inheritance
• Multiple child classes inherit from one parent

Example:
class Animal:
def sound(self):
print("Animal makes sound")

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

class Cat(Animal):
def sound(self):
print("Cat meows")

dog = Dog()
cat = Cat()
[Link]()
[Link]()
Hybrid Inheritance

• Combination of single, multiple, and hierarchical


inheritance patterns

• Powerful but complex, must be managed


carefully to avoid confusion
Real-World Applications
✔ GUI frameworks – widgets share common
methods
✔ Game development – characters share traits and
actions
✔ Banking systems – different accounts share
transaction logic
✔ Content management systems – posts, pages,
and users share attributes
Best Practices
✔ Use inheritance when objects share common
behavior or interface
✔ Avoid deep hierarchies that complicate the
codebase
✔ Document class relationships clearly
✔ Use super() wisely to extend methods
✔ Prefer composition over inheritance where
appropriate
Common Pitfalls
❌ Overusing inheritance instead of composition
❌ Creating deep, hard-to-maintain inheritance
chains
❌ Ignoring the use of super()
❌ Poor documentation leading to maintenance
issues
❌ Overcomplicating operator overloads or
overridden methods
Summary
• Inheritance helps reuse code and create
structured relationships
• Supports single, multiple, multilevel,
hierarchical, and hybrid inheritance
• Enables method overriding and the use of
super()
• Widely applicable in real-world applications
• Best practices ensure maintainable and scalable
designs
ASSESSMENT

[Link]

You might also like