Python Class Inheritance-How to Create a Derived Class

Inheritance implements Code Reuse. It is a feature of Object Oriented Programming paradigm wherein  an existing class is used to create a new class. The new class inherits properties and methods of the base class. It can be enhanced in functionality by adding more methods and properties. If the method with same name exists in base class, derived class method overrides them. Python Class Inheritance is a useful technique of writing clean and reusable Python programs.

This is the hierarchy that we are going to implement. So, let’s learn how it is done!!

Python Class Inheritance- Example

Create Base Class

To implement Python Class Inheritance first you need a base class. It can be a new class that passes on its properties and methods to multiple derived classes. If you have already created a class that you want to extend by adding more properties and methods, it can work as a base class.

To implement generalization concept of OOPs in your programs of applications, you can create a new base class. It must be created with common features shared by many derived classes. The base class is declared just like any other Python class.

Example

A base class User is created with properties – name, password and department. Methods include the constructor __ini__ to initialize objects of User Class, __str__ method to print the object properties and check method to check if the user name and password matches to grant them access.

Code:

class user:
    def __init__(self,name=None, password=None,department=None):
        self.name=name
        self.password=password
        self.department=department
    def __str__(self):
        return (self.name+" "+self.password+" "+self.department)
    def check(self, pwd):
        if (self.password==pwd):
            print("access granted")
        else:
            print("Access denied")

#creating objects and testing class created above
user1= user("Amy","Adams","Production")
user2= user("Bob","Black","Sales")
user3= user("Cathy","Keans","HR")
print(user1)
print(user2)
print(user3)

user2.check("kkkk")
Example Python Class Inheritance

Create Derived Class/ Child Class

Now that the base class or parent class is created, we can create child classes from it. A child class is a new class that inherits properties and methods from the base class. If you wish you can inherit same properties and method of base class by using keyword pass in child class declaration.

You can add new properties in the derived class in addition to the inherited ones. For this you need to declare them in derived class.

A child class Admin is created with rights as an additional property. Methods include the constructor __ini__   to initialize objects of Admin class, __str__ method to print the object properties and ChangeRights method to change access rights of a user.

Python Class Inheritance using pass keyword

class user:
    
    def __init__(self,name=None, password=None,department=None):
        self.name=name
        self.password=password
        self.department=department
    def __str__(self):
        return (self.name+" "+self.password+" "+self.department)
    def check(self, pwd):
        if (self.password==pwd):
            print("access granted")
        else:
            print("Access denied")

class Admin(user):
    pass #inheriting everything from base class. No new methods or properties added
            
#creating objects and testing classes created above
user1= Admin("Amy","Adams","Production")
user2= Admin("Bob","Black","Sales")
user3= Admin("Cathy","Keans","HR")
print(user1)
print(user2)
print(user3)

user2.check("kkkk")
Inheritance with pass

Python Class Inheritance with New Properties and Methods in Derived Class

class user:
    def __init__(self,name=None, password=None,department=None):
        self.name=name
        self.password=password
        self.department=department
    def __str__(self):
        return (self.name+" "+self.password+" "+self.department)
    def check(self):
        if (self.loginList[self.name]==self.password):
            print("access granted")
        else:
            print("Access denied")

class admin(user):
    def __init__(self,name=None, password=None, deptt=None,rights=""):
        user.__init__(self, name, password, deptt)
        self.rights=rights
    def __str__(self):
        suptext=super().__str__()
        return(suptext+ " "+self.rights)
    def ChangeRights(self, newRights=None):
        self.rights=newRights
             
#creating objects and testing classes created above
adm= admin("Amy,","Adams","Production","View")
print(adm)

adm.ChangeRights("Read/Write/Execute")
print("after changing rights")
print(adm)
Derived class with added properties and methods

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *