Course Content

Single Inheritance | Python Tutorials For Absolute Beginners In Hindi #60

Let's just begin this Tutorial by opening our PyCharm and creating a new file.

 

 

 

We are going to name our file oops7.py here. 

 

In today’s tutorial, we are going to learn about Single inheritance in Python. Before discussing the concept of inheritance in Python, we should have some knowledge about the word itself. Inheritance means to receive something as an heir from one's parents or ancestors. The concept of inheritance is very similar in cases of classes where a class inherits all the properties and methods of its previous class that it is inheriting from.

"Inheritance is the ability to define a new class(child class) that is a modified version of an existing class(parent class)"

Syntax:

class Parent_class_Name:
#Parent_class code block
class Child_class_Name(Parent_class_name):
#Child_class code block

The above syntax consists of two classes declared. One is the parent class or by other means, the base class, and the other one is the child class which acts as the derived class.

Two common terms related to inheritance are as follows:

  • Parent: The parent class is the one that is giving access to its methods or properties to the child class or derived class.
  • Child: Child class is the one that is inheriting methods and properties from the parent class.

The class that is inheriting i.e. the child class not only can inherit all the functionality of the parent class but can also add its functionalities too. As we have already discussed that each class can have its constructors and methods, so in case of inheritance the child class can make and use its constructor and also can use the constructor of the parent class. We can simply construct it as we did for the parent class but OOP has provided us with a simple and more useful solution known as Super().

We will be discussing super() and overriding in our Super() and Overriding In Classes tutorial of the course.

Single inheritance exists when a class is only derived from a single base class. Or in other words when a child class is using the methods and properties of only a single parent class then single inheritance exists. Single inheritance and Multiple inheritance are very similar concepts, the only major difference is the number of classes. We will see Multiple Inheritance in our next tutorial.

Different forms of Inheritance:

  1. Single inheritance: When a child class inherits from only one parent class then it is called single inheritance.
  2. Multiple inheritance: When a child class inherits from multiple parent classes then it is called multiple inheritance

Below is an example of single inheritance in python.

class Parent():
    def first(self):
        print('Parent function')
        
class Child(Parent):
    def second(self):
        print('Child function')

object1 = Child()
object1.first()
object1.second()

Output: 

Parent function

Child function

Advantages of inheritance:

  • It promotes code’s reusability as we do not have to copy the same code again to our new class.
  • It makes the program more efficient.
  • We can add more features to our already built class without modifying it or changing its functionality.
  • It provides a representation of real-world relationships.

In this tutorial, we have discussed the Inheritance concept. Inheritance is among the most significant concepts in object-oriented programming technique which provides code reusability, readability and helps in building optimized and efficient code.

Code as described/written in the video


class Employee:
    no_of_leaves = 8

    def __init__(self, aname, asalary, arole):
        self.name = aname
        self.salary = asalary
        self.role = arole

    def printdetails(self):
        return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"

    @classmethod
    def change_leaves(cls, newleaves):
        cls.no_of_leaves = newleaves

    @classmethod
    def from_dash(cls, string):
        return cls(*string.split("-"))

    @staticmethod
    def printgood(string):
        print("This is good " + string)


class Programmer(Employee):
    no_of_holiday = 56
    def __init__(self, aname, asalary, arole, languages):
        self.name = aname
        self.salary = asalary
        self.role = arole
        self.languages = languages


    def printprog(self):
        return f"The Programmer's Name is {self.name}. Salary is {self.salary} and role is {self.role}.The languages are {self.languages}"



harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")

shubham = Programmer("Shubham", 555, "Programmer", ["python"])
karan = Programmer("Karan", 777, "Programmer", ["python", "Cpp"])
print(karan.no_of_holiday)

Comments(1)

shiv29 4 months, 3 weeks ago
Harry Bhai, I have been watching your videos from beginning wish me luck to be a best programmer. Though I am not from IT or technical background. I have studied management but somehow it is not helping me in my career so from last more than year I have changed my field and working as Data Analyst. I am very grateful that I have come cross with your channel on you tube while searching videos for python. Your videos are helping me lot. Thanks bhai. You are doing absolutely fantabulous job.

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here