Inheritance and Abstraction 5th Sem
Inheritance and Abstraction 5th Sem
Lesson Plan
Inheritance and
Inheritance and
Lesson Plan
Abstraction
Lesson
Lesson Plan
Plan
Abstraction
Polymorphismand
Polymorphism and
Encapsulation
Encapsulation
Java + DSA
Topic to covered:
2. Multiple inheritance : One child class may inherit from several parent classes when there is multiple
inheritance
Child Class
In this example, we have two parent classes, ParentClass1 and ParentClass2, each with their own methods. The
ChildClass inherits from both ParentClass1 and ParentClass2.
The ChildClass can now access methods from both parent classes. It can call method1() from ParentClass1,
method2() from ParentClass2, as well as its own child_method().
When creating an instance of ChildClass and calling the methods, you will see the respective outputs as
mentioned in the comments.
Multiple inheritance allows classes to inherit and combine the behavior of multiple parent classes, providing
flexibility in designing complex class hierarchies. However, it’s important to carefully consider the design and
potential complexities that can arise when using multiple inheritance.
Java + DSA
Diamond problem
The diamond problem is a specific issue that can arise in programming languages that support multiple
inheritance, including Python. It occurs when a class inherits from two or more classes that have a common
base class. This can lead to ambiguity in method resolution, causing conflicts and making it unclear which
version of a method should be used.
To mitigate the diamond problem, Python uses a method resolution order (MRO) algorithm called C3
linearization. The MRO determines the order in which the base classes are searched for a method or attribute. It
follows a specific set of rules to ensure a consistent and unambiguous order.
Here’s an example that demonstrates the diamond problem and how Python resolves it:
Class A:
Method in B
In this example, we have four classes: A, B, C, and D. Both B and C inherit from A, and D inherits from both B and
C. All classes define a method called method().
When d.method() is called, Python follows the MRO to determine the order in which the base classes are
searched for the method. In this case, the MRO is D -> B -> C -> A. So, Python looks for the method() in class D
first, then in B, then in C, and finally in A.
As a result, the output is “Method in B”. This is because B is the first class in the MRO that defines the method(),
so its implementation is used.
Python’s MRO algorithm effectively resolves the diamond problem by providing a well-defined order for method
resolution. However, it’s important to be aware of potential conflicts and understand the MRO to design your
class hierarchy and override methods appropriately.
3. Multilevel Inheritance : A class inherits from a child class or derived class under multilevel inheritance.
Think of three classes: A, B, and C. Superclass A, child class B, and child class C are all subclasses of A. In other
words, multilevel inheritance is the term used to describe a set of classes.
Parent Class
Child Class 1
Child Class 2
4. Hierarchical Inheritance : A single parent class gives rise to multiple child classes under hierarchical
inheritance. To put it another way, we can say that there is one parent class and several child classes.
Parent Class
Non-Parametrized
Parameterized
Default Constructor
Constructor Constuctor
Example-
5. Hybrid inheritance : When inheritance consists of multiple types or a combination of different
inheritance is called hybrid inheritance.
Parent
Class
Child Child
Class 1 Class 2
Child
Class 3
Abstraction in python : In Python, abstraction is a key idea in object-oriented programming (OOP) that
allows you to describe complicated systems by hiding unneeded details and exposing just essential aspects.
It aids in the management of programme complexity by breaking it down into smaller, more manageable
sections.
Classes and objects are used to accomplish abstraction. A class is a blueprint for constructing objects,
whereas an object is a class instance.
The class defines the properties (attributes) and behaviours (methods) of the class's objects.Abstraction allows
you to construct abstract classes and methods that give a high-level interface without providing the
implementation specifics.
Abstract classes cannot be instantiated and must be subclassed. They may contain abstract methods that are
declared but not implemented in the abstract class itself. Subclasses are in charge of implementing these
abstract methods.
Java + DSA
Abstract Class :
To declare an Abstract class, we firstly need to import the abc module. Let us look at an example.
Here, abs_class is the abstract class inside which abstract methods or any other sort of methods can be
defined.
As a property, abstract classes can have any number of abstract methods coexisting with any number of other
Abstraction in Python
Here,Absclass is the abstract class that inherits from the ABC class from the abc module. It contains an
abstract method task() and a print() method which are visible by the user. Two other classes inheriting from
this abstract class are test_class and example_class. Both of them have their own task() method (extension of
After the user creates objects from both the test_class and example_class classes and invoke the task()
method for both of them, the hidden definitions for task() methods inside both the classes come into play.
These definitions are hidden from the user. The abstract method task() from the abstract class Absclass is
But when the print() method is called for both the test_obj and example_obj, the Absclass’s print() method is
THANK
YOU !