0% found this document useful (0 votes)
5 views6 pages

Python Inheritance All Examples

The document provides an overview of inheritance in Python, detailing five types: Single, Multiple, Multilevel, Hierarchical, and Hybrid Inheritance. Each type is illustrated with examples showcasing how classes can derive from one another to inherit properties and methods. The examples demonstrate practical applications of inheritance in object-oriented programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Python Inheritance All Examples

The document provides an overview of inheritance in Python, detailing five types: Single, Multiple, Multilevel, Hierarchical, and Hybrid Inheritance. Each type is illustrated with examples showcasing how classes can derive from one another to inherit properties and methods. The examples demonstrate practical applications of inheritance in object-oriented programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Inheritance in Python – Easy Examples

Types of Inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Single Inheritance
Example 1:
class Person:
def show_person(self):
print("I am a person")

class Student(Person):
def show_student(self):
print("I am a student")

s = Student()
s.show_person()
s.show_student()

Example 2:
class Animal:
def eat(self):
print("Animal eats")

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

d = Dog()
[Link]()
[Link]()

Example 3:
class Vehicle:
def start(self):
print("Vehicle starts")
class Car(Vehicle):
def drive(self):
print("Car drives")

c = Car()
[Link]()
[Link]()

Multiple Inheritance
Example 1:
class Father:
def father_quality(self):
print("Father is strong")

class Mother:
def mother_quality(self):
print("Mother is caring")

class Child(Father, Mother):


pass

Example 2:
class Camera:
def take_photo(self):
print("Taking photo")

class Phone:
def call(self):
print("Calling")

class Smartphone(Camera, Phone):


pass

Example 3:
class Writer:
def write(self):
print("Writing")

class Reader:
def read(self):
print("Reading")

class Student(Writer, Reader):


pass

Multilevel Inheritance
Example 1:
class Animal:
def eat(self):
print("Eating")

class Dog(Animal):
def bark(self):
print("Barking")

class Puppy(Dog):
def weep(self):
print("Weeping")

Example 2:
class Vehicle:
def start(self):
print("Vehicle starts")

class Car(Vehicle):
def drive(self):
print("Car drives")

class ElectricCar(Car):
def charge(self):
print("Charging")

Example 3:
class Person:
def show(self):
print("I am a person")

class Employee(Person):
def work(self):
print("I work")

class Manager(Employee):
def manage(self):
print("I manage")
Hierarchical Inheritance
Example 1:
class Animal:
def eat(self):
print("Animal eats")

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

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

Example 2:
class Shape:
def draw(self):
print("Drawing shape")

class Circle(Shape):
def circle(self):
print("Circle")

class Rectangle(Shape):
def rectangle(self):
print("Rectangle")

Example 3:
class Teacher:
def teach(self):
print("Teaching")

class MathTeacher(Teacher):
def math(self):
print("Teaching Math")

class ScienceTeacher(Teacher):
def science(self):
print("Teaching Science")
Hybrid Inheritance
Example 1:
class Father:
def f(self):
print("Father")

class Mother:
def m(self):
print("Mother")

class Child(Father, Mother):


pass

class Student(Child):
def s(self):
print("Student")

Example 2:
class Engine:
def engine(self):
print("Engine")

class Vehicle(Engine):
def vehicle(self):
print("Vehicle")

class Electric:
def battery(self):
print("Battery")

class Car(Vehicle, Electric):


pass

Example 3:
class A:
def showA(self):
print("Class A")

class B(A):
def showB(self):
print("Class B")

class C(A):
def showC(self):
print("Class C")

class D(B, C):


pass

You might also like