100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

python inheritance

Rating
-
Sold
-
Pages
28
Uploaded on
13-10-2021
Written in
2021/2022

It gives the clear idea about the concept python inheritance .which helps to write exams in first semister of engineering college

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
October 13, 2021
Number of pages
28
Written in
2021/2022
Type
Class notes
Professor(s)
Satyanarayana
Contains
All classes

Subjects

Content preview

UNIT -5::PP
======================================================================
Inheritance: Introduction, inheriting classes, types of inheritance,overridingmethods,abstract
classes and interfaces.
===================================================================
Python Inheritance
Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides code
reusability to the program because we can use an existing class to create a new class instead of
creating it from scratch.

In inheritance, the child class acquires the properties and can access all the data members and
functions defined in the parent class. A child class can also provide its specific implementation to the
functions of the parent class.

In python, a derived class can inherit base class by just mentioning the base in the bracket after the
derived class name. Consider the following syntax to inherit a base class into the derived class.
Inheritance allows us to define a class that inherits all the methods and properties from another
class.

Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.




Syntax
class derived-class(base class):
<class-suite>
A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the
following syntax.

Syntax
class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
<class - suite>

Example
class Person: Mike Olsen
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

,class Student(Person):
pass

x = Student("Mike", "Olsen")
x.printname()

Example
class Animal: Output:
def speak(self): dog barking
print("Animal Speaking") Animal Speaking
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
d = Dog()
d.bark()
d.speak()

Add the __init__() Function
So far we have created a child class that inherits the properties and methods from its parent.
We want to add the __init__() function to the child class (instead of the pass keyword).

Note: The __init__() function is called automatically every time the class is being used to create a
new object.

Example
Add the __init__() function to the Student class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
When you add the __init__() function, the child class will no longer inherit the
parent's __init__() function.

Note: 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:

Example
class Person: Mike Olsen
def __init__(self, fname, lname): age: 20
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

class Student(Person):
def __init__(self, age,fname, lname):
Person.__init__(self,fname, lname)
self.age=age

, x = Student(20,"Mike", "Olsen")
x.printname()
print("age:",x.age)
Now we have successfully added the __init__() function, and kept the inheritance of the parent
class, and we are ready to add functionality in the __init__() function.
===============================================================================
Use the super() Function
Python also has a super() function that will make the child class inherit all the methods and
properties from its parent:
class Person:
def __init__(self, fname, lname): Mike Olsen
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)

x = Student("Mike", "Olsen")
x.printname()

Add Properties
Example
class Person: 2019
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019

x = Student("Mike", "Olsen")
print(x.graduationyear)

In the example below, the year 2019 should be a variable, and passed into the Student class when
creating student objects. To do so, add another parameter in the __init__() function:

Example
Add a year parameter, and pass the correct year when creating objects:
class Person: 2019
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
$10.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
baby30

Get to know the seller

Seller avatar
baby30 Stuvia
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
4 year
Number of followers
1
Documents
12
Last sold
4 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions