PYTHON
PROGRAMMING
LESSON
Abstraction & Inheritance
TABLE OF CONTENTS
01
inheritance
02
Abstraction
What is Inheritance?
The method of inheriting the properties of parent class
into a child class is known as inheritance. It is an OOP
concept. Following are the benefits of inheritance.
1. Code reusability- we do not have to write the same
code again and again, we can just inherit the
properties we need in a child class.
2. It represents a real world relationship between parent
class and child class.
3. It is transitive in nature. If a child class inherits
properties from a parent class, then all other
sub-classes of the child class will also inherit the
properties of the parent 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.
01
Create a Parent Class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and
then execute the printname method:
x = Person("John", "Doe")
x.printname()
02
Create a Child Class
Create a class named Student, which will inherit the
properties and methods from the Person class:
class Student(Person):
Pass
Note: Use the pass keyword when you do not want to
add any other properties or methods to the class
03
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.
Add the __init__() function to the Student class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
Example of properties :
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
04
Add 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 Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
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.
05
Add Properties & Methods
Add a property called graduationyear to the Student
class:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
Add a method called welcome to the Student class:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname,
self.lastname, "to the class of",
self.graduationyear)
EXERCISE
1. Définir une classe Book avec les
attributs suivants : Title, Author (Nom
complet), Price.
2. Définir un constructeur ayant
comme attributs: Title, Author, Price.
3. Définir la méthode View() pour
afficher les informations d'une instance
object Book.
4. Ecrire un programme pour tester la
classe Book.
Solution
# Question 1
class Book:
# Question 2
def __init__(self , Title , Author , Price):
self.Title = Title
self.Author = Author
self.Price = Price
# Question 3
def view(self ):
return ("Book Title: " , self.Title , "Book Author: " , self.Author, "Book Price: " , self.Price)
# Question 4
MyBook = Book("Python Crash Course" , "Eric Matthes" , "23 $")
print( MyBook.view())
# La sortie est : ('Book Title: ', 'Python Crash Course', 'Book Author: ', 'Eric Matthes', 'Book Price: ', '23 $')
What is Abstraction?
Abstraction in python is defined as a process of
handling complexity by hiding unnecessary
information from the user. This is one of the core
concepts of object-oriented programming (OOP)
languages. That enables the user to implement
even more complex logic on top of the provided
abstraction without understanding or even
thinking about all the hidden
background/back-end complexity.
Abstraction Classes
Python provides the abc module to use the abstraction in the Python program. Let's see the following
syntax.
Syntax
1. from abc import ABC
2. class ClassName(ABC):
Unlike the other high-level language, Python doesn't provide the abstract class itself. We need to import the abc
module, which provides the base for defining Abstract Base classes (ABC). The ABC works by decorating methods of
the base class as abstract.
AbstractionClasses
1. # abstract base class work
1. t= Tesla ()
2. from abc import ABC, abstractmethod
2. t.mileage()
3. class Car(ABC):
3.
4. def mileage(self):
4. r = Renault()
5. pass
5. r.mileage()
6.
7. class Tesla(Car): 6.
8. def mileage(self): 7. d = Duster()
9. print("The mileage is 30kmph") 8. d.mileage()
10. class Renault(Car):
11. def mileage(self):
12. print("The mileage is 25kmph ")
13. class Duster(Car):
14. def mileage(self):
15. print("The mileage is 24kmph ")
Thank You