Day12
2 min readSep 23, 2019
Inheritance : Inheritance is one of the oops mechanism which allow to invoking all base class properties into child properties.
There are is creating two types relation-ship in between parents and child classes
1. IS-A(inheritance relationship)
2. Has-A(Composition relationship)
There are 3-types of inheritance.
1. Single inheritance
2. Multiple inheritance
3. Multi-level inheritance
Single inheritance : In this types of inheritance which is use parents properties invoke in child class :
Is-A(inheritance relationship)
Example Is-A(inheritance relationship):-
class Parent:
parent_name = ""
child_name = ""
def show_parent(self):
print(self.parent_name)
class Child(Parent):
def show_child(self):
print(self.child_name)
ch1 = Child()
ch1.parent_name = "Sachin"
ch1.child_name = "Arjuna"
ch1.show_parent()
ch1.show_child()
Has-A (Composition-relationship)
class Book:
def __init__(self, name,author,publication, price, genre):
self.name=name
self.author=author
self.publication=publication
self.price=price
self.genre=genre
def show_details(self):
return"{} {} {} {} {}".format(self.name, self.author, self.publication,self.price, self.genre)
class Member:
def __init__(self,name, id, address):
self.name=name
self.id=id
self.address=address
class Library:
working_hours = 24
def __init__(self, name, location):
self.name = name
self.location = location
@classmethod
def change_time(cls, new_time):
cls.working_hours = new_time
class Librarian:
def __init__(self, name, gender, qualification, experience):
self.name = name
self.gender = gender
self.qualification = qualification
self.experience = experience
self.list_of_books = []
def find_total_books(self):
return len(self.list_of_books)
def open_lib(self):
return "library opened"
def issue_book(self, book_name):
self.list_of_books.remove(book_name)
return "book issued"
def add_book(self, book_name):
self.list_of_books.append(book_name)
return "book added"
def impose_fine(self):
return "pay fine"
def close_lib(self):
return "library closed"
class Book:
def __init__(self, name, author, publication, price, genre):
self.name = name
self.author = author
self.publication = publication
self.price = price
self.genre = genre
def show_details(self):
return "{} {} {} {} {}".format(self.name, self.author, self.publication, self.price, self.genre)
class Member:
def __init__(self, name, id, address):
self.name = name
self.id = id
self.address = address
def borrow_book(self):
return "The book is borrowed"
def return_book(self):
return "The book is returned"
def pay_fine(self):
return "The fine to be paid"
sachin = Member("sachin", "#001", "bangalore")
saurabh = Member("saurabh", "#002", "bangalore")
rsagarwal = Librarian("rsagarwal", True, "3rd grade", 50)
rdsharma = Book("rdsharma", "sharmaji", "shawarma", 100, "shorma")
rsagarwal.list_of_books.append(rdsharma)
library = Library("420", "bangalore")
print("Total number of books in library", rsagarwal.find_total_books())
Super() : It provides the access to those methods of the parent class which have been overridden in a child class that inherits from it.
example:
class Square:def __init__(self, side):self.side = sidedef area(self):return self.side * self.sideclass Cube(Square):def area(self):face_area = self.side * self.sidereturn face_area * 6def volume(self):face_area = self.side * self.sidereturn face_area * self.side