0% found this document useful (0 votes)
7 views18 pages

Inheritance

The document explains inheritance in object-oriented programming, highlighting the relationship between base (parent) classes and derived (child) classes. It covers syntax for defining classes, method overriding, and the use of the super() function to access parent class methods. Additionally, it provides examples of single inheritance and demonstrates how derived classes can inherit and override attributes and methods from their parent classes.

Uploaded by

bhavani.p
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)
7 views18 pages

Inheritance

The document explains inheritance in object-oriented programming, highlighting the relationship between base (parent) classes and derived (child) classes. It covers syntax for defining classes, method overriding, and the use of the super() function to access parent class methods. Additionally, it provides examples of single inheritance and demonstrates how derived classes can inherit and override attributes and methods from their parent classes.

Uploaded by

bhavani.p
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

Inheritance:

The new class is called derived (or child) class and the one
from which it inherits is called the base (or parent) class.

Inheritance follows “IS-A” relationship.


Rectangle is a Polygon
Triangle is a Polygon

Dog IS an animal. Cat also IS an animal. Hence,


animal is the base class, while dog and cat are
inherited classes.
Syntax While defining the child class, the

name of the parent class is put in the


parentheses in front of it, indicating
the relation between the two.

class Parent:
def __init__(self, a, b):
self.a=a
self.b=b
def display(self):
print("a=",self.a)
print("b=",self.b)

class child(Parent): def sum(self):


print("Sum=",self.a+self.b) ch=child(10,20) [Link]() [Link]()

# Base class
class Dog: Inheritance
# Class attribute
species = 'mammal'

# Instance attributes
def __init__(self, name, age): # Derived class (inherits from Dog class)
[Link] = name
class Bulldog(Dog):
[Link] = age
def run(self, speed):
# instance method (
def description(self): return "{} runs {}".format([Link], speed)
(
(
return "{} is {} years old".format([Link],

# Derived class inherits attributes and


# behavior from the parent class Jim = Bulldog("Jim", 12)
[Link]) print([Link]())
(

# instance method (
( # Derived class has specific attributes # and
def speak(self, sound):
behavior as well
return "{} says {}".format([Link], sound) print([Link]("slowly"))
• When you add the __init__() function, the child
class will no longer inherit the parent's __init__()
function.

• The child's __init__() function overrides the


inheritance of the parent's __init__() function.
• To keep the inheritance of the parent's __init__()
function, add a call to the parent's __init__()
function:
Python also has a super() function that will make the child class inherit all the
methods and properties from its parent:
By using the super() function, you do not have to use the name
of the parent element, it will automatically inherit the methods
and properties from its parent.
Create a base class “quadrilateral” and define a function to
find the perimeter. Derive a class “rectangle” from the base
class. Define a function area in it.
A) Find the perimeter of the quadrilateral.
B) Find the area and perimeter of the rectangle

Output:

Method Overriding
Methods of the parent class are available for use in the
inherited class. However, if needed, we can modify the
functionality of any base class method.
For that purpose, the inherited class contains a new definition
of a method (with the same name and the signature already
present in the base class). Naturally, the object of a new class
will have access to both methods, but the one from its own
class will have precedence when invoked. This is called method
overriding.
class Animal():
Method Overriding Base class

class Animal(): print("ANIMAL CREATED") def


def __init__(self): whoami(self):
def __init__(self): print("I am an animal") def eat(self):
print("I am eating")
print("ANIMAL CREATED") class Dog(Animal): Inherited Animal class

def whoami(self): def __init__(self): Animal.__init__(self)


print("I am an animal") print("Dog created")

def eat(self): def eat(self): Possible to override methods

print("I am eating") print("I am a dog and eating")

myanimal =Animal() def bark(self): Possible to add methods


[Link]() [Link]() mydog =Dog()
print("WOOF!") mydog .whoami()
mydog .eat()
mydog .bark()

Inheritance - Review
• To inherit a class from another class, put the
superclass name in parentheses after the class
name.
• A class that inherits from another class is called a subclass. • A class that
is inherited from is called a superclass.
• If a class inherits from another with the same attributes or methods, it
overrides them.
Single Inheritance
Single level inheritance

enables a derived class to

inherit characteristics from a

single parent class


Output
Rohit
2024
“BCA”
123
The function super is a useful inheritance-related function that
refers to the parent class. It can be used to find the method with a
certain name in an object's
superclass.

Output
2
1

Same as [Link]()

You might also like