KEMBAR78
09 - Inheritance, Abstract Classes | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
5 views8 pages

09 - Inheritance, Abstract Classes

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

09 - Inheritance, Abstract Classes

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

INHERITANCE

Inheritance : Inheritance is a mechanism in which object of one class acquires some or all the
properties and behavior of object of another class .
The idea behind inheritance in Python is that it enables creation of new classes that are built
upon existing classes . when a new class inherits from another class then the new class gets
some or all the properties and behavior of the old class. if required any number of methods
and fields can be added that are specific to the new class The new class is called as super
class/base class/parent class and the old one is called as sub class/derived class/child class. The
advantage of inheritance is code reusability. inheritance represents IS-A relationship .
Types of inheritance :
Single inheritance : one base class and one derived class
A

B
Example:
Employee

SalariedEmployee
Multilevel inheritance:
one base class, one derived class and one or more intermediate base classes
A

C
In the above inheritance path, B is the base class for Derived class C , and B is a derived class of
base class A . B acts as a base class for some derived class and derived class for some other
base class hence B is called as an intermediate base class
Example
Person

Employee

SalariedEmployee

Hierarchical inheritance: one base class with two or more derived classes
A

B C

Example:
Employee

SalariedEmployee HourlyPaidEmployee
Multiple inheritance: one derived class with more than one base class.
A B

C
Hybrid inheritance: combination of two or more inheritance is called as Hybrid inheritance
Super(): The super() builtin returns a proxy object (temporary object of the superclass) that
allows us to access methods of the base class
Uses of super():
1) allows us to avoid using base class name explicitly
2) working with multiple inheritance

class Person:
def __init__(self,name,age):
self.name=name
self.age=age
class Student(Person):
def __init__(self,name,age,email):
super().__init__(name,age)
self.email=email
s1=Student('Alan Parkar',25,'par@gmail.com')
print(s1.name)
print(s1.age)
print(s1.email)

Method Overriding
The child classes in Python can inherit methods and attributes from the parent class. We can
redefine certain methods and attributes specifically to fit the child class, which is known as
Method Overriding. Polymorphism allows us to access these overridden methods and attributes
that have the same name as the parent class.

from math import pi


class Shape:
def __init__(self, name):
self.name = name
def area(self):
pass
def fact(self):
return "This is a two-dimensional shape."
def __str__(self):
return self.name
class Square(Shape):
def __init__(self, length):
super().__init__("Square")
self.length = length
def area(self):
return self.length**2
def fact(self):
return "All squares have four sides equal "
class Circle(Shape):
def __init__(self, radius):
super().__init__("Circle")
self.radius = radius
def area(self):
return pi*self.radius**2

a = Square(4)
b = Circle(7)
print(b)
print(b.fact())
print(a.fact())
print(b.area())

Output

Circle
This is a two-dimensional shape.
All squares have four sides equal
153.93804002589985

Here, we can see that the methods such as __str__(), which have not been overridden in the
child classes, are used from the parent class.
Due to polymorphism, the Python interpreter automatically recognizes that the fact() method
for object a(Square class) is overridden. So, it uses the one defined in the child class.
On the other hand, since the fact() method for object b isn't overridden, it is used from the
Parent Shape class.
ABSTRACT CLASSES
In Python methods are categorized into two types.
1. Concrete methods
2. Abstract methods
Concrete methods: methods with implementation are called concrete methods.
Abstract method: method having empty implementation is called abstract method.
child class is responsible to provide implementation for parent class abstract methods.
By declaring abstract method in the parent class, we can set up the rules to the child class .
every child class must provide the implementation for all the abstract methods of its parent
class.

Abstract class: In a class if there is at least one abstract method then that class must be
declared as an abstract class. Object creation is not allowed for abstract class. The purpose of
declaring an abstract class is to prevent the object creation. Even though there are no abstract
methods in a class still we can declare that class as abstract class.
An abstract class can have zero or more abstract methods.
Example:
Account

CurrentAccount SavingsAccount

The common properties and behavior of CurrentAccoutn and SavingsAccount are taken in
Account class . we wish to prevent the object creation for Account class then we can declare
Account class as abstract even though there is no abstract method in it.
from abc import ABC,abstractmethod
class Figure(ABC):
def __init__(self,s):
self.s=s
def getNumOfSides(self):
return self.s
@abstractmethod
def getArea(self):
pass
class Rectangle(Figure):
def __init__(self,l,b):
super().__init__(4)
self.l=l
self.b=b
def getArea(self):
return self.l*self.b
class Triangle(Figure):
def __init__(self,b,h):
super().__init__(3)
self.b=b
self.h=h
def getArea(self):
return (1/2)*self.b*self.h
r=Rectangle(1.2,3.5)
print('Number of sides of a rectangle is ', r.getNumOfSides())
print('Area of the rectangle is :',r.getArea())
t=Triangle(1.5,2.3)
print('Number of sides of a triangle is ', t.getNumOfSides())
print('Area of the triangle is :',t.getArea())

output:

Number of sides of a rectangle is 4


Area of the rectangle is : 4.2
Number of sides of a triangle is 3
Area of the triangle is : 1.7249999999999999

You might also like