- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Inheritance Definition
Inheritance allows a class (child) to acquire properties and methods of another class (parent).
This promotes:
- Code reusability
- Hierarchy / structure
- Cleaner and scalable programs
Basic Inheritance
class Animal:
def speak(self):
print("Animals make sound")
class Dog(Animal): # Dog inherits from Animal
pass
d = Dog()
d.speak()
Explanation
Animalis the parent/base class.Dogis the child/derived class.- Dog gets the
speak()method without writing it again.
Single Inheritance
class Parent:
def show(self):
print("This is parent class")
class Child(Parent):
def display(self):
print("This is child class")
c = Child()
c.show()
c.display()
📝 Child gets access to both:
➡ show() (from Parent)
➡ display() (its own)
Multilevel Inheritance
class A:
def feature1(self):
print("Feature 1")
class B(A):
def feature2(self):
print("Feature 2")
class C(B):
def feature3(self):
print("Feature 3")
c = C()
c.feature1()
c.feature2()
c.feature3()
📝 Chain of inheritance: A → B → C
Class C inherits everything.
Multiple Inheritance
class Father:
def bike(self):
print("Father's bike")
class Mother:
def car(self):
print("Mother's car")
class Child(Father, Mother):
pass
obj = Child()
obj.bike()
obj.car()
📝 Child receives methods of both parents.
Hierarchical Inheritance
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
pass
class Cat(Animal):
pass
d = Dog()
c = Cat()
d.sound()
c.sound()
📝 One parent → Multiple children.
Using super() to Access Parent Data
class Parent:
def __init__(self):
print("Parent constructor")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child constructor")
c = Child()
📝 super() is used to call parent methods/constructors.