0% found this document useful (0 votes)
48 views7 pages

Class (Poly INHERITANE)

Inheritance allows a child class to inherit properties from a parent class. There are different types of inheritance including single inheritance where a child class inherits from one parent class, multiple inheritance where a child inherits from multiple parent classes, multilevel inheritance where a derived class acts as a parent class for another derived class, and hierarchical and hybrid inheritance which combine different inheritance techniques. Polymorphism allows child classes to provide their own implementation of methods from the parent class.

Uploaded by

Gautam Sai
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)
48 views7 pages

Class (Poly INHERITANE)

Inheritance allows a child class to inherit properties from a parent class. There are different types of inheritance including single inheritance where a child class inherits from one parent class, multiple inheritance where a child inherits from multiple parent classes, multilevel inheritance where a derived class acts as a parent class for another derived class, and hierarchical and hybrid inheritance which combine different inheritance techniques. Polymorphism allows child classes to provide their own implementation of methods from the parent class.

Uploaded by

Gautam Sai
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/ 7

Polymorphism  

With Inheritance

The process of inheriting the properties of the parent class into a child class is
called inheritance.

The existing class is called a base class or parent class

The new class is called a subclass or child class or derived class.

 The reusability of code because we can use the existing class to create a new class
instead of creating it from scratch.

The child class acquires all the data members, properties, and functions from
the parent class

childa class can also provide its specific implementation to the methods of the
parent class
class BaseClass:
Body of base class class
DerivedClass(BaseClass):
Body of derived class
Polymorphism  With Inheritance

The type of inheritance are listed below: 1.Single inheritance Multiple Inheritance
Multilevel inheritance Hierarchical Inheritance Hybrid Inheritance
child class inherits from one child class can inherit Derived class of a class is base
a single-parent class. from multiple parent classe class of a nother derived class

more than one child class is


derived from a single parent class consists of multiple types 

inheritance
Single Inheritance

class Vehicle: # Base class


def Vehicle_info(self):
print('Inside Vehicle class')

class Car(Vehicle): # Child class


def car_info(self):
print('Inside Car class')

# Create object of Car


car = Car()

# access Vehicle's info using car object


car.Vehicle_info() Inside Vehicle class
car.car_info() Inside Car class
Multiple-Inheritance

class Person: # Parent class 1


def person_info(self, name, age):
print('Inside Person class')
print('Name:', name, 'Age:', age)

class Company: # Parent class 2


def company_info(self, company_name, location):
print('Inside Company class')
print('Name:', company_name, 'location:', location)
class Employee(Person, Company): # Child class
def Employee_info(self, salary, skill):
print('Inside Employee class')
print('Salary:', salary, 'Skill:', skill)
emp = Employee() # Create object of Employee
Inside Person class
emp.person_info('Jessa', 28) Name: Jessa Age: 28
Inside Company class
emp.company_info('Google', 'Atlanta') Name: Google location: Atlanta

Inside Employee class


Salary: 12000 Skill: Machine Learning
emp.Employee_info(12000, 'Machine Learning')
Multilevel inheritance

class Vehicle: # Base class


def Vehicle_info(self):
print('Inside Vehicle class')

Class Car(Vehicle): # Child class


def car_info(self):
print('Inside Car class')

class SportsCar(Car): # Child class


def sports_car_info(self):
print('Inside SportsCar class')
# Create object of SportsCar
s_car = SportsCar()
# access Vehicle's and Car info using SportsCar object
s_car.Vehicle_info() Inside Vehicle class

s_car.car_info() Inside Car class


s_car.sports_car_info() Inside SportsCar class
Hierarchical Inheritance
class Vehicle:
def info(self):
print("This is Vehicle")

class Car(Vehicle):
def car_info(self, name):
print("Car name is:", name)
class Truck(Vehicle):
def truck_info(self, name):
print("Truck name is:", name)

obj1 = Car()
obj1.info() This is Vehicle

obj1.car_info('BMW') Car name is: BMW

obj2 = Truck() This is Vehicle

Truck name is: Ford


obj2.info()
Hybrid Inheritance
class Vehicle:
def vehicle_info(self):
print("Inside Vehicle class")

class Car(Vehicle):
def car_info(self):
print("Inside Car class")
class Truck(Vehicle):
def truck_info(self):
print("Inside Truck class")
# Sports Car can inherits properties of Vehicle and Car
class SportsCar(Car, Vehicle):
def sports_car_info(self):
print("Inside SportsCar class")

s_car = SportsCar()
Inside Vehicle class
s_car.vehicle_info()
Inside Car class
s_car.car_info()
s_car.sports_car_info() Inside SportsCar class

You might also like