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

CMPP 613 1ST Assignment

on point

Uploaded by

Ajijolas I.S
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)
22 views6 pages

CMPP 613 1ST Assignment

on point

Uploaded by

Ajijolas I.S
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/ 6

In Object-Oriented Programming (OOP), inheritance allows a

class (subclass) to inherit attributes and methods from


another class (superclass). Different types of inheritance
relationships can be implemented in OOP. Here are some
common types of inheritance along with examples to
illustrate each type:
1. Single Inheritance:
o In a single inheritance, a subclass inherits from
only one superclass.
o Example:

plaintext

class Animal:

def __init__(self, name):

self.name = name

class Dog(Animal):

def __init__(self, name, breed):

super().__init__(name)

self.breed = breed

o In this example, the Dog class inherits from


the Animal class.
2. Multilevel Inheritance:
o In multilevel inheritance, a subclass inherits from a
superclass, and another subclass inherits from the
first subclass.
o Example:

plaintext

class Animal:

def speak(self):

print("Animal speaks")

class Dog(Animal):

def bark(self):

print("Dog barks")

class Labrador(Dog):

def color(self):

print("Labrador is brown")

o Here, the Labrador class inherits from


the Dog class, which in turn inherits from
the Animal class.
3. Hierarchical Inheritance:
o In hierarchical inheritance, multiple subclasses
inherit from the same superclass.
o Example:

plaintext

class Shape:

def draw(self):

pass

class Circle(Shape):

def draw(self):

print("Circle drawn")

class Rectangle(Shape):

def draw(self):

print("Rectangle drawn")

o Both Circle and Rectangle classes inherit from


the Shape class in this example.
4. Multiple Inheritance:
o In multiple inheritance, a subclass inherits from
multiple superclasses.
o Example:
plaintext

class A:

def method_A(self):

print("Method A")

class B:

def method_B(self):

print("Method B")

class C(A, B):

def method_C(self):

print("Method C")

o The C class inherits from both classes A and B,


allowing it to access methods from both.
5. Hybrid Inheritance:
o Hybrid inheritance is a combination of two or more
types of inheritance.
o Example:

plaintext
class A:

def method_A(self):

print("Method A")

class B(A):

def method_B(self):

print("Method B")

class C(A):

def method_C(self):

print("Method C")

class D(B, C):

def method_D(self):

print("Method D")

o The D class exhibits hybrid inheritance by


inheriting from classes B and C, which themselves
inherit from class A.
Each type of inheritance serves a specific purpose in OOP
and allows for code reuse, extensibility, and organization of
classes in a hierarchical structure

You might also like