Python

Python Inheritance: Unlock the Super Power of Object-Oriented Programming

Python Inheritance is the most important concept in programming it might be c, java, python..It’s used almost in every application, inheritance has a lot of benefits even.

Inheritance is a fundamental feature of object-oriented programming (OOP) that allows you to create a new class by extending an existing one. In Python, you can implement inheritance by creating a subclass that inherits the attributes and methods of its superclass.

To define a subclass, you simply need to specify the superclass in the definition using parentheses. The subclass can then access the attributes and methods of the superclass using the super() function.

Inheritance is a powerful feature of Python that allows you to reuse code, reduce code duplication, and create specialized classes that share common functionality.

Inheritance usage and detailed explanation is provided in this blog with the help of real time examples.

Let us try to see an example and understand the concept of python inheritance.

Python Inheritance Video Tutorial :

In this video tutorial let us try to see the detailed implementation of inheritance in python.

class Student:
    studentName = 'Ravi'

    def printstudentname(self):
        print(self.studentname)

now try to create a object for the class Student and call the methods and variables.

s1 = Student()

s1.printstudentname()

output:

Ravi

Now try to create another class Professor

class Professor:
    professorName = 'Shyam'

    def printprofessorname(self):
        print(self.professorname)

Now create a object for Professor class

p1 = Professor()

p1.printprofessorname()

Here we can access the variables and methods of Professor class.

Single Inheritance :

Single inheritance, which means that a subclass can only inherit from a single superclass. To create a subclass, you simply define it by specifying the superclass in parentheses after the class name.

The subclass then inherits all the attributes and methods of the superclass. You can override inherited methods in the subclass or add new methods and attributes specific to the subclass.

Considering the above example let us see single inheritance. We are extending Professor class with Student class

class Student:
    studentName = 'Ravi'

    def printstudentname(self):
        print(self.studentname)

class Professor(Student):
    professorName = 'Shyam'

    def printprofessorname(self):
        print(self.professorname)

And now try to create an object of professor class that can access both the class Student, Professor variables and methods.

Multilevel Inheritance :

Multilevel inheritance is a type of inheritance in Python where a subclass is derived from a superclass, which in turn is derived from another superclass. This means that the subclass inherits attributes and methods from both the immediate superclass and the super-superclass.

To implement multilevel inheritance in Python, you need to create a chain of classes where each class is derived from the class above it. You can define methods and attributes in each class that are specific to that class or inherited from the superclass.

Considering the above example let us see multi-level inheritance. We are extending Professor class, Student class with college class

class Student:
    studentName = 'Ravi'

    def printstudentname(self):
        print(self.studentname)

class Professor(Student):
    professorName = 'Shyam'

    def printprofessorname(self):
        print(self.professorname)

class College(Professor):
    collegeName = 'UV University'

    def printcollegename(self):
        print(self.collegeName)

And now try to create an object of college class that can access both the class Student, Professor variables and methods.

p1 = College()

p1.printprofessorname() // professor

p1.printstudentname()   // student

p1.printcollegename()  // college

output :

Shyam
Ravi
UV University

Multiple Inheritance :

Multiple inheritance is a type of inheritance in Python where a subclass is derived from two or more superclasses. This means that the subclass inherits attributes and methods from all the superclasses.

To implement multiple inheritance in Python, you need to define a class that inherits from two or more classes using a comma-separated list of the superclass names in parentheses after the class name. You can then define methods and attributes in the subclass that are specific to that subclass or inherited from the superclasses.

Now create one more class so that we can understand multiple inheritance.

class College(Professor, Student):
    collegeName = 'Stanley'

    def printcollegename(self):
        print(self.collegename)

Now create a object for College class which can access both Professor and Student class.

c1 = College()

c1.printprofessorname() // professor

c1.printstudentname()   // student

c1.printcollegename()   // college

output:

Shyam
Ravi
Stanley

Inheritance allows you to create specialized subclasses that inherit the common attributes and methods of a superclass, making your code more modular and reusable. It also enables you to organize your code in a hierarchical manner, where subclasses inherit from other subclasses, creating a tree-like structure.

If you have any query’s in this tutorial on Python Inheritance do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..

Python Inheritance
Abhishek

Share
Published by
Abhishek

Recent Posts

HTML: The Evolution and Power of Unleashed Web Language and Modern too

IntroductionInitial StagesEvolutionChallenging Other LanguagesCurrent TrendsAI and HTMLConclusion Introduction HTML, or HyperText Markup Language, is the…

2 months ago

Increase in 80C in Budget 2024 ?

Increase in 80CDemands and Discussions: Increase in 80C Section 80C of the Income Tax Act…

2 months ago

ChatGPT 4o Unleashing the Power of GPT A Comprehensive Guide

IntroductionWhat is ChatGPT-4?Key Features of ChatGPT-4Enhanced Natural Language UnderstandingImproved Response GenerationVersatilityApplications of ChatGPT-4Customer SupportContent CreationEducational…

4 months ago

APJ Abdul Kalam Biography: A Tribute to India’s Powerful Missile Man

APJ Abdul Kalam Biography :Childhood :Academics :Professional Career :Achievements : APJ Abdul Kalam Biography :…

7 months ago

Srinivasa Ramanujan Biography: Exploring the Genius

We value your feedback! Please share your thoughts on this blog on Srinivasa Ramanujan Biography…

7 months ago

Interim Budget 2024: What to Expect Any Rise

Interim Budget 2024 :Importance :Key Points :Rise in 80 C ?Standard Deduction : Interim Budget…

8 months ago

This website uses cookies.