Python > Object-Oriented Programming (OOP) in Python > Inheritance > Single Inheritance
Single Inheritance Example: Animal and Dog
This snippet demonstrates single inheritance in Python. A Dog
class inherits from an Animal
class, inheriting its properties and methods. This showcases a fundamental OOP concept where a class can inherit characteristics from a single parent class.
Basic Code Example
The Animal
class has a constructor (__init__
) that initializes the animal's name and a speak
method. The Dog
class inherits from Animal
using class Dog(Animal):
. The Dog
's constructor calls the Animal
's constructor using super().__init__(name)
to initialize the inherited name
attribute. The Dog
class also overrides the speak
method to return "Woof!". Finally, an instance of Dog
is created, demonstrating the inherited and overridden behaviors.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Generic animal sound"
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return "Woof!"
dog = Dog("Buddy", "Golden Retriever")
print(dog.name)
print(dog.breed)
print(dog.speak())
Concepts Behind the Snippet
Inheritance: Inheritance allows a class (child class or subclass) to inherit attributes and methods from another class (parent class or superclass). This promotes code reuse and establishes an 'is-a' relationship (e.g., a Dog 'is a' Animal).
Single Inheritance: In single inheritance, a class can inherit from only one parent class.super()
function: The super()
function is used to call methods from the parent class. This is crucial for initializing inherited attributes and calling overridden methods.
Method Overriding: Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.
Real-Life Use Case
Consider a software system for managing employees in a company. You could have a base class Employee
with attributes like name
, employee_id
, and methods like calculate_salary()
. Then, you could have subclasses like Manager
, Engineer
, and Salesperson
, each inheriting from Employee
. Each subclass can override the calculate_salary()
method to implement salary calculation logic specific to that type of employee. The Manager
class might add attributes like team_size
, and the Salesperson
class might add attributes like sales_target
. This demonstrates how inheritance can model real-world hierarchies and relationships, promoting code reuse and maintainability.
Best Practices
super()
consistently: Always use super()
to initialize inherited attributes when overriding the constructor in a subclass. This ensures that the parent class's initialization logic is executed correctly.Dog
object should be usable anywhere an Animal
object is expected.
Interview Tip
Be prepared to explain the benefits of inheritance, such as code reuse and polymorphism. Also, be ready to discuss the potential drawbacks, such as increased complexity and the 'fragile base class' problem. Demonstrate your understanding of super()
and method overriding with examples.
When to Use Them
Use inheritance when you have a clear 'is-a' relationship between classes. For example, a 'Car' is a type of 'Vehicle', or a 'Circle' is a type of 'Shape'. Inheritance promotes code reuse and makes your code more organized and easier to understand when dealing with hierarchical relationships.
Memory Footprint
Inheritance introduces a small memory overhead because subclasses inherit the attributes and methods of their superclasses. However, this overhead is generally negligible compared to the benefits of code reuse and organization. The memory footprint of an object includes the space required to store its attributes and a pointer to its class, which in turn contains information about its methods and superclasses.
Alternatives
Composition: Instead of inheriting, a class can contain instances of other classes as attributes. This allows you to reuse code without creating a strict 'is-a' relationship. Composition is often preferred over inheritance when you want to combine the functionality of multiple classes in a more flexible way.
Mixins: Mixins are small classes that provide specific functionality to other classes through multiple inheritance. This allows you to add functionality to a class without creating a deep inheritance hierarchy.
Pros
Cons
FAQ
-
What is the purpose of the
super()
function?
Thesuper()
function is used to call methods from the parent class. It's typically used in the subclass's constructor (__init__
) to initialize inherited attributes and in overridden methods to call the parent class's implementation. -
What is method overriding?
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. This allows the subclass to customize the behavior of inherited methods. -
When should I use inheritance?
Use inheritance when you have a clear 'is-a' relationship between classes and you want to reuse code and establish a hierarchical relationship. However, be mindful of the potential drawbacks of inheritance, such as increased complexity and tight coupling.