Python OOP — Inheritance

Python OOP — Inheritance

Inheritance is a key concept in Object-Oriented Programming. It enables us to create a new class from an existing class. The new class is a specialized version of the existing class and it inherits all the non-private variables and methods of the existing class. When should we use inheritance? The answer is that whenever we come across an IS A relationship between objects.

Inheritance is a key concept in Object-Oriented Programming. It enables us to create a new class from an existing class. The new class is a specialized version of the existing class and it inherits all the non-private variables and methods of the existing class.

When should we use inheritance? The answer is that whenever we come across an IS Arelationship between objects.

  • Python IS A programming language
  • Tesla IS A car
  • Google IS A company

From above examples, we can build new classes by extending existing classes (programming language, car, company)

Syntax & Terminologies

when we create a new class based on an existing class, we use the following terminology:

  • Parent Class ( Super Class)
  • Child Class (Sub Class): The class is the one that inherits the superclass
Class ParentClass:

Class ChildClass (ParentClass):

Example

class Vehicle:
    def __init__(self, make, color, model):
        self.make = make
        self.color = color
        self.model = model
    def printDetails(self):
        print ("Manufacturer: ", self.make)
        print ("Color: ", self.color)
        print ("Model: ", self.model)
class Car(Vehicle):
    def __init__(self, make, color, model, doors):
        Vehicle.__init__(self, make, color, model)
        self.doors = doors
    def printCarDetails(self):
        self.printDetails()
        print ("Doors: ", self.doors)

In this example, Vehicle is a parent class and we implement a Car class that will extend from this Vehicle class.

Super() Function

It is used in a child class when we implement inheritance and it refers to the parent class without explicitly naming it.

It is mainly used in three contexts

1. Accessing Parent Class Properties

If we have a field named fuelCap defined inside a Vehicle class to keep track of the fuel capacity of a vehicle. If we implement a Car class that will extend from the Vehicle class and we declare a class property inside the Car class with the same name, fuelCap but different value, we can refer to the fuelCap of the parent class inside the child class, we will then use the super() function.

python inheritance oop-concepts python-programming oop

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

OOP in Python | Python OOP Concepts | Object Oriented Programming Python

OOP in Python will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.

Python OOPS Concepts | Python OOP Tutorial | Python Classes and Objects

Learn Python OOPS. We'll understand the different object-oriented programming concepts and how Python implements them by creating classes and objects. You will get an idea about inheritance, encapsulation, and polymorphism and perform a hands-on demo on these concepts.

Python OOPs Concept | Object Oriented Programming Python

In this Python tutorial for beginners, you’ll learn the basics of object-oriented programming in Python. Object-oriented programming (OOP) is a method of structuring a program by bundling related properties and behaviors into individual objects

Python Programming Concepts | Python Concepts | Python Programming

This Edureka Live video on 'Python Programming Concepts' will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.

Guide to Python Programming Language

Guide to Python Programming Language