Unit V - Inheritance
Unit V - Inheritance
Python Programming
Using Problem Solving Approach
Reema Thareja
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
Example:
Example:
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
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
15
16
18
19