Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views4 pages

Inhetience Example

The document provides examples of different types of inheritance in Python, including single, multiple, multilevel, hierarchical, and hybrid inheritance. Each section includes class definitions and demonstrates how methods can be inherited and utilized in subclasses. The examples illustrate the concept of method resolution order and how different inheritance structures can be implemented in object-oriented programming.

Uploaded by

SHAHIDHA
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)
9 views4 pages

Inhetience Example

The document provides examples of different types of inheritance in Python, including single, multiple, multilevel, hierarchical, and hybrid inheritance. Each section includes class definitions and demonstrates how methods can be inherited and utilized in subclasses. The examples illustrate the concept of method resolution order and how different inheritance structures can be implemented in object-oriented programming.

Uploaded by

SHAHIDHA
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
You are on page 1/ 4

1.

Single Inheritance

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

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

d = Dog()
d.speak()
d.bark()
------------------------------------------------------------------

class Vehicle:
def move(self):
print("Vehicle moves")

class Car(Vehicle):
def horn(self):
print("Car horn")

c = Car()
c.move()
c.horn()

2. Multiple Inheritance

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

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

class Child(Father, Mother):


def own_skills(self):
print("Child: Painting")

c = Child()
c.skills() # Father’s skill due to method resolution order
c.own_skills()

--------------------------------------------------------------------
class Writer:
def write(self):
print("Writes stories")

class Artist:
def draw(self):
print("Draws paintings")

class Person(Writer, Artist):


pass

p = Person()
p.write()
p.draw()

3. Multilevel Inheritance

class Animal:
def eat(self):
print("Eats food")

class Mammal(Animal):
def walk(self):
print("Walks on land")

class Dog(Mammal):
def bark(self):
print("Barks")

d = Dog()
d.eat()
d.walk()
d.bark()

----------------------------------------------------------------------
class ElectronicDevice:
def power(self):
print("Needs power")

class Computer(ElectronicDevice):
def compute(self):
print("Computes data")

class Laptop(Computer):
def portable(self):
print("Easy to carry")

l = Laptop()
l.power()
l.compute()
l.portable()
4. Hierarchical Inheritance

class Animal:

def eat(self):
print("Eats food")

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

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

dog = Dog()
cat = Cat()
dog.eat()
cat.eat()
dog.bark()
cat.meow()
--------------------------------------------------------

class Vehicle:
def move(self):
print("Vehicle is moving")

class Bike(Vehicle):
def ride(self):
print("Riding bike")

class Car(Vehicle):
def drive(self):
print("Driving car")

b = Bike()
c = Car()
b.move()
c.move()
b.ride()
c.drive()
5. Hybrid Inheritance

class A:
def feature1(self):
print("Feature 1")

class B(A):
def feature2(self):
print("Feature 2")

class C:
def feature3(self):
print("Feature 3")

class D(B, C):


def feature4(self):
print("Feature 4")

d = D()
d.feature1()
d.feature2()
d.feature3()
d.feature4()
----------------------------------------------------------------

class School:
def location(self):
print("Located in city")

class Teacher(School):
def teach(self):
print("Teaches subjects")

class Staff(School)s:
def manage(self):
print("Manages school")

class Principal(Teacher, Staff): # Hybrid: Multilevel + Multiple


def lead(self):
print("Leads school")

p = Principal()
p.location()
p.teach()
p.manage()
p.lead()

You might also like