Python Inheritance and Polymorphism
Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP).
Inheritance:
Inheritance is a mechanism that allows a new class to be defined based on an existing class. The new class is called a subclass or derived class and the existing class is called the base class or superclass. The subclass inherits all the attributes and methods of the base class and can also add new attributes and methods or override the ones inherited from the base class.
For example, let's say we have a base class called Animal
with the attributes name
and species
and a method make_sound
that prints a message indicating the sound that the animal makes:
rubyCopy codeclass Animal: def __init__(self, name, species): self.name = name self.species = species def make_sound(self): print(f"{self.name} makes a sound")
Now, let's say we want to create a new class Dog
that represents a specific type of animal. We can do this by creating a subclass of Animal
and adding a new method bark
that makes the dog bark:
pythonCopy codeclass Dog(Animal): def bark(self): print(f"{self.name} barks")
We can now create an instance of the Dog
class and call its methods:
makefileCopy codedog = Dog("Fido", "Canis lupus familiaris") dog.make_sound() # Fido makes a sound dog.bark() # Fido barks
As we can see, the Dog
class has inherited the attributes name
and species
and the method make_sound
from the Animal
class, and has added its own method bark
.
Polymorphism:
Polymorphism is a concept that allows objects of different classes to be treated as objects of a common class. This is achieved through method overloading and method overriding.
Method overloading is a mechanism where multiple methods with the same name but different arguments are defined in a class. This allows the same method to be called with different argument lists, which can result in different behavior depending on the arguments passed.
Method overriding is a mechanism where a subclass provides a new implementation for a method defined in its base class. This allows the subclass to override the behavior of the method inherited from the base class.
For example, let's say we have a class Shape
with an abstract method area
that returns the area of the shape:
rubyCopy codeclass Shape: def area(self): raise NotImplementedError("Subclass must implement this method")
Now, let's say we want to create two subclasses Rectangle
and Circle
that represent specific types of shapes. The Rectangle
class will implement the area
method to return the area of a rectangle and the Circle
class will implement the area
method to return the area of a circle:
class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2
rect = Rectangle(10, 20) print(rect.area()) # 200 circ = Circle(5) print(circ.area()) # 78.5
Leave a Comment