0% found this document useful (0 votes)
54 views19 pages

Unit V - Inheritance

Uploaded by

amlakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views19 pages

Unit V - Inheritance

Uploaded by

amlakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

Python Programming
Using Problem Solving Approach

Reema Thareja

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


CHAPTER 10
Inheritance

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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 derived by inheriting/extending the generalized classes.
3

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Inheriting Classes in Python
The syntax to inherit a class can be given as,
class DerivedClass(BaseClass): body_of_derived_class

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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: Example:
statement block
class Base2:
statement block
class Derived (Base1, Base2):
statement block

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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,
Example:
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
Pass

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 9
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 10
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 11
METHOD OVERRIDING

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 12


© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 13
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.

14

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Inheritance vs Composition

15

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Composition or Containership or Complex Objects — Example

16

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Abstract Classes and Interfaces
It is possible to create a class which cannot be instantiated. This means that 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.
17

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Abstract Classes And Interfaces - Example

18

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Metaclass
A metaclass is the class of a class. While a class defines how an instance of the class behaves, a metaclass, on the other
hand, defines how a class behaves. Every class that we create in Python, is an instance of a metaclass.
For example, type is a metaclass in Python. It is itself a class, and it is its own type. Although, you cannot make an exact
replica of something like type, but Python does allow you to create a metaclass by making a subclass type.
A metaclass is most commonly used as a class-factory. It allows programmers to create an instance of the class by calling
the class,
Python allows you to define normal methods on the metaclass which are like classmethods, as they can be called on the
class without an instance. You can even define the normal magic methods, such as __add__, __iter__ and __getattr__, to
implement or change how the class behaves.

19

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.

You might also like