Our Best Offer Ever!! Summer Special - Get 3 Courses at 24,999/- Only. Read more
Hire Talent (HR):+91-9791 444 054


Python Inheritance

Python Inheritance

Python Inheritance

Inheritance is the most important oops concept which provides the facility to maintain the class level hierarchy in which we can achieve these many features listed below.

  • Reusability in code.
  • The relationship between classes to maintain and map the code model to the real world.
  • Nature of acquiring the property of parent to child.

Reusability in code:

Inheritance reusability we can understand by an example, suppose we have to design a real-world use case for Employee details for any organization. Then we should think about the Employee object characteristics.

In this use case we can keep all Employee common details in Employee as parent class and all other classes like a developer, tester, HR, Sales all other departments will inherit Employee class so we no need to write the common details in each class.

Relationship between classes to maintain and map the code model to the real world:

Before designing any software or an application which will be used to work on real-world use cases then any programmer needs to think before designing the model for that application.

Thinking about the classes we can think about is-a relationship for example if the software is about Bank then Bank will be parent class and other banks branches will be subclasses for that. A conclusion of this is to observe the “is-a” relationship is nothing that any programmer needs to find out for designing the real-life use case.

Nature of acquiring the property of parent to child:

This is the base of inheritance which is behind all the thing achieved in programming either it is Relationship between classes to maintain and map the code model to the real world or Reusability in code. We can understand this by an example.

For example, we have Vehicle as parent class the subclass may be Car, Bus, Bike and all.

So we can easily map it with is a relationship.

Like: Bike -> is-a -> Vehicle and Car -> is-a -> Vehicle

So the parent common prosperities like break, tier, and gear will be by default in subclasses.

So it is necessary that we should implement the inheritance between those classes.

Code Example python Inheritance:


class Vehicle:
def __init__(self, name):
self.name = name
def name_of_car(self):
print('name of the car{}'.format(self.name))
def spee_of_car(self, speed):
print('speed of the car is{}'.format(speed))

class Car(Vehicle):
def has_auto_speech(self, flag):
print('has auto speech {}'.format(flag))

Class Car (Vehicle): this is the way to inherit the parent class in python.

Click Here-> Get Python Training with Real-time Projects

Besant Technologies WhatsApp