Slide_8
Slide_8
Slide_8
1
Introduction
The technique of creating a new class from an existing class is called inheritance. The old
or existing class is called the base class and the new class is known as the derived class or
sub-class. The derived classes are created by first inheriting the data and methods of the
base class and then adding new specialized data and functions in it. In this process of
inheritance, the base class remains unchanged. The concept of inheritance is used to
implement the is-a relationship. For example, teacher IS-A person, student IS-A person;
while both teacher and student are a person in the first place, both also have some
distinguishing features. So all the common traits of teacher and student are specified in
the Person class and specialized features are incorporate in two separate classes- Teacher
and Student. Inheritance which follows a top down approach to problem solving. In top-
down approach, generalized classes are designed first and then specialized classes are
2
derived by inheriting/extending the generalized classes.
Inheriting Classes in Python
The syntax to inherit a class can be given as,
class DerivedClass(BaseClass): body_of_derived_class
Example:
3
Polymorphism and Method Overriding
Polymorphism refers to having several different forms. It is one of the key features of OOP. It enables the
programmers to assign a different meaning or usage to a variable, function, or an object in different contexts.
While inheritance is related to classes and their hierarchy, polymorphism, on the other hand, is related to
methods. When polymorphism is applied to a function or method depending on the given parameters, a
particular form of the function can be selected for execution. In Python, method overriding is one way of
implementing polymorphism.
Example: In the last example, the __init__() method was defined in all the three classes. When this happens, the method in
the derived class overrides that in the base class.
This means that __init__() in Teacher and Student gets preffered over __init__() method in the person class
Instead of writing persn. __init__(self, name, age), we coulod have also written super().__init__(self, name, age)
Super() is a built in function that denotes the base class. So when we invoke a method using the super () function,
then the parent version of the method is called 4
Multiple Inheritance
When a derived class inherits features from more than one base class, it is called multiple inheritance. The
derived class has all the features of both the base classes and in addition to them can have additional new
features.The syntax for multiple inheritance is similar to that of single inheritance and can be given as:
class Base1:
statement block
class Base2:
statement block
class Derived (Base1, Base2):
statement block
5
class Base1():
def __init__(self):
print("Base 1 called") Base 1 called
super().__init__() Base 2 called
class Base2(): Base 3 called
def __init__(self):
print("Base 2 called")
super().__init__()
class Base3():
def __init__(self):
print("Base 3 called")
super().__init__()
class Derived(Base1, Base2, Base3):
def __init__(self):
super().__init__()
D = Derived()
6
class Base1():
def __init__(self):
print("Base 1 called")
super().__init__()
Base 2 called
class Base2():
Base 3 called
def __init__(self):
Base 1 called
print("Base 2 called")
super().__init__()
class Base3():
def __init__(self):
print("Base 3 called")
super().__init__()
class Derived(Base2, Base3, Base1):
def __init__(self):
super().__init__()
D = Derived()
7
class Base1(): Base 3 called
def __init__(self): Base 2 called
super().__init__() Base 1 called
print("Base 1 called")
class Base2():
def __init__(self):
super().__init__()
print("Base 2 called")
class Base3():
def __init__(self):
super().__init__()
print("Base 3 called")
class Derived(Base1, Base2, Base3):
def __init__(self):
super().__init__()
D = Derived()
8
Multi-Level Inheritance
The technique of deriving a class from an already derived class is called multi - level inheritance.
The syntax for multi-level inheritance can be given as,
class Base: Example:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
Pass
9
Multi-path Inheritance
Deriving a class from other derived classes that are in turn derived from the same base class is called multi-path
inheritance.
Example:
10
Abstract Classes and Interfaces
It is possible to create a class which cannot be instantiated. This means that you cannot create objects of that class.
Such classes could only be inherited and then an object of the derived class was used to access the features of the
base class. Such a class was known as the abstract class.
An abstract class corresponds to an abstract concept. For example, a polygon may refer to a rectangle, triangle or
any other closed figure. Therefore, an abstract class is a class that is specifically defined to lay a foundation for other
classes that exhibits a common behavior or similar characteristics. It is primarily used only as a base class for
inheritance.
Since an abstract class, is an incomplete class, users are not allowed to create its object. To use such a class,
programmers must derive it and override the features specified in that class.
An abstract class just serves as a template for other classes by defining a list of methods that the classes must
implement. In Python, we use the NotImplementedError to restrict the instantiation of a class. Any class that has the
NotImplementedError inside method definitions cannot be instantiated. 11
Abstract Classes And Interfaces - Example
12
Composition or Containership or Complex Objects
Complex objects are objects that are built from smaller or simpler objects. For example, a car is built using a
metal frame, an engine, some tyres, a transmission, a steering wheel, and several other parts. Similarly, a
computer system is made up of several parts such as CPU, motherboard, memory, and so on. This process of
building complex objects from simpler ones is called composition or containership.
In object-oriented programming languages, object composition is used for objects that have a has-a
relationship to each other. For example, a car has-a metal frame, has-an engine, etc. A personal computer
has-a CPU, a motherboard, and other components.
Until now, we have been using classes that have data members of built-in type. While this worked well for
simple classes, for designing classes that simulate real world applications, programmers often need data
members that belong to other simpler classes.
13
Inheritance vs Composition
14
Composition or Containership or Complex Objects — Example
15