w3resource

What is inheritance in Python object-oriented programming?

Inheritance in Python object-oriented programming

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a subclass or derived class) to inherit properties and behaviors from another class (called a superclass or base class). In Python, a subclass can inherit attributes and methods from its superclass, allowing code reuse and creating a hierarchical relationship between classes.

Subclasses can either add their own methods and attributes or override those in the superclass.

Inheritance is represented by syntax-

class SubclassName(BaseClassName):

Where SubclassName is the name of the subclass and BaseClassName is the name of the superclass. The subclass inherits the superclass's properties using this syntax.

Python: Key points about inheritance

  • Code Reusability: Inheritance promotes code reusability by allowing subclasses to inherit attributes and methods from a common superclass. This reduces code duplication and makes the codebase more maintainable.
  • Subclass and Superclass Relationship: Inheritance creates relationships between classes, where a subclass "is" specialized version of the superclass. For example, a "Tiger" class can be a subclass of a "Animal" class.
  • Method Overriding: Subclasses can override superclass methods. When a method is called on an instance of the subclass, Python first looks for the method in the subclass. If not found, it looks for a method in the superclass.
  • Single Inheritance: Python supports single inheritance, which means that a subclass can inherit only from one superclass. However, a superclass can have multiple subclasses.
  • Multiple Levels of Inheritance: Subclasses can also act as superclasses for other subclasses, creating multiple levels of inheritance.

Example of inheritance in Python:

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

    def speak(self):
        return "Unknown sound"

class Lion(Animal):  # Lion class inherits from Animal class
    def speak(self):
        return "Roar!"

class Tiger(Animal):  # Tiger class inherits from Animal class
    def speak(self):
        return "Growl!"

# Create instances of subclasses
lion_instance = Lion("King")
tiger_instance = Tiger("Tony")

# Call the speak method of the subclasses
print(lion_instance.speak())  # Output: Roar!
print(tiger_instance.speak())  # Output: Growl!

In the example above, we have a superclass "Animal" with an attribute 'name' and a method "speak()". The subclasses 'Lion' and 'Tiger' inherit from the "Animal" class and override the 'speak()' method to provide their specific sound.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-interview/what-is-inheritance-in-python-object-oriented-programming.php