This tutorial is a continuation of our Python Classes section. Here we’ll learn about more advanced concepts like inheritance, polymorphism and encapsulation in Advanced Python Classes.

See the individuals link for each concept for a more detailed explanation complete with examples.


Inheritance:

Inheritance is the concept of having a class inherit properties from another class. The parent class is the one from which the properties are inherited, and the child class is the one inheriting the properties. The parent class can also be called the base class, and the child class can be called the derived class.

Why use inheritance?

  • Inheritance improves the re-usability of code. Instead of having to write down all that code again, we simply change a one line statement that automatically inherits all functions and variables from the parent class.
  • Inheritance is useful in representing real world relationships as well. Such as that between a Parent and Child, or a Teacher and Student.
class student(human):
    def __init__(self, grade, name, gender):
        self.grade = grade
        self.name = name
        self.gender = gender

    def displaygrade(self):
        print(self.grade)


obj = student(6, "Bob", "Male")
obj.displayname()
obj.displaygrade()
obj.displaygender()

Although not shown in this example, you can see that obj, an instance of student is using functions that were not defined in the student class. This is thanks to Inheritance.

Follow this link to understand more about Inheritance in Python.


Encapsulation:

Encapsulation is the concept of restricting the direct access of variables and methods. Python Encapsulation allows the only the object itself to change its variables, just to avoid any accidental change. We call such variables and methods, private or protected, depending on the protection level given.

A Class “encapsulates” all the variables and methods in it, allowing only the class object modify it. You may find this concept similar to variable scope (local and global) in Functions.

Protected

Protected members are those which can only be accessed by the class itself, or it’s sub-classes (Child classes). These members cannot be accessed from the main Python body. You can declare a protected member by using a single underscore "_" as a prefix.

Private

Private members are those which can only be accessed in the main class itself. These members cannot be accessed by any sub classes or by the Main Python body. You can declare a private member by using a double underscore "__" as a prefix.


Polymorphism

The word Polymorphism means the ability to take various forms. In Python Classes, Polymorphism allows us to define methods in the child class with the same name as defined in their parent class.

Here is an example of polymorphism in classes.

class teacher:
    def info(self):
        print("...Relevant Teacher information...")

class student:
    def info(self):
        print("...Relevant Student information...")
        
obj1 = teacher()
obj2 = student()

obj1.info()
obj2.info()
...Relevant Teacher information...
...Relevant Student information...

As you can see above, both classes posses a function called info, but both have different outputs. This is polymorphism. Same name, but different contents and roles.


This marks the end of our Advanced Python Classes guide. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked below in the comments section.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments