Editor settings

General settings

Save settings

Learn to program with Python

Class Inheritance #

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit attributes and behaviors from another class. In Python, classes can be organized in a hierarchical structure using inheritance, creating a parent-child relationship between classes.

The class that is being inherited from is called the "parent class" or "superclass," and the class that inherits from it is called the "child class" or "subclass." The child class inherits all the attributes and methods defined in the parent class.

A subclass can be created with the class keyword, followed by the subclass name, followed by parentheses of superclasses to inherit from. See the Dog class below as an example:

class Animal
    alive = True

    def __init__(self, name):
        self.name = name

    def eat(self):
        print(f"The animal {self.name} is eating")

class Dog(Animal):
    pass

In the example above, Animal is the parent class, and Dog is the child class that inherits from the Animal class. Any objects created from the Dog class will have the same properties and methods as those created from the Animal class.

bird = Animal("Bird")
dog = Dog("Clark")
bird.eat() # The animal Bird is eating
dog.eat() # The animal Clark is eating
print(dog.alive) # True

Exercise #

  • Create a new subclass MyUpperStr that inherits from our MyStr class.

Tests #

  • MyUpperStr is a subclass of the parent class MyStr
Output will be displayed here