Inheritance
Inheritance is a key feature of object-oriented programming that allows classes to inherit attributes and methods from other classes. In Python, inheritance is implemented using the class declarations, and it enables a new class to be based on an existing class, known as the parent or base class. The new class is referred to as the child or derived class.
The derived class can access the attributes and methods of the base class, as well as add its own attributes and methods. This can save time and reduce redundancy when creating multiple related classes. For example, a derived class can inherit common attributes and methods from a base class, while adding its own specific attributes and methods.
To implement inheritance in Python, we use the class
keyword followed by the derived class name, followed by the base class name in parentheses. Here is an example:
class BaseClass:
def __init__(self):
self.x = 5
def print_x(self):
print(self.x)
class DerivedClass(BaseClass):
def __init__(self):
super().__init__()
self.y = 10
def print_y(self):
print(self.y)
In this example, we have defined two classes, BaseClass
and DerivedClass
. The DerivedClass
is derived from the BaseClass
. The BaseClass
contains an __init__
method and a print_x
method. The DerivedClass
has an __init__
method and a print_y
method, and it inherits the __init__
and print_x
methods from the BaseClass
using the BaseClass
in parentheses in the class declaration.
The super()
function is used in the __init__
method of the DerivedClass
to call the __init__
method of the BaseClass
. This ensures that any initialization done in the BaseClass
is also done when creating an instance of the DerivedClass
.
Now we can create an instance of the DerivedClass
and access its attributes and methods:
d = DerivedClass()
d.print_x() # prints 5
d.print_y() # prints 10
In this example, the instance d
of the DerivedClass
can access the print_x
method inherited from the BaseClass
and the print_y
method specific to the DerivedClass
.
In conclusion, inheritance is a powerful feature of Python that allows for the creation of efficient and organized code. By reusing code from existing classes, we can save time and reduce redundancy when creating related classes.
Polymorphism
Polymorphism is a key feature of object-oriented programming that allows objects of different classes to be treated as if they were the same type of object. In Python, polymorphism is implemented through the use of method overloading and method overriding.
Method overloading is when a method has multiple definitions, and the correct definition is selected at runtime based on the number or types of arguments provided. Python does not support method overloading natively, but we can use default arguments or variable arguments to achieve similar functionality.
Method overriding is when a derived class has a method with the same name and signature as a method in the base class. When the method is called on an instance of the derived class, the implementation in the derived class is used instead of the one in the base class.
Here’s an example of polymorphism in Python using method overriding:
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
print("The animal makes a sound.")
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
def animal_sound(animal):
animal.make_sound()
my_dog = Dog("Buddy")
my_cat = Cat("Fluffy")
animal_sound(my_dog) # prints "Woof!"
animal_sound(my_cat) # prints "Meow!"
In this example, we have a base class called Animal
that has an __init__
method that takes in a name
argument, and a make_sound
method that prints a generic animal sound. We then create two derived classes, Dog
and Cat
, which override the make_sound
method with their own implementations of the method.
We then define a function called animal_sound
that takes an object of type Animal
and calls its make_sound
method. This function can be called with an object of any derived class of Animal
, and the appropriate implementation of the make_sound
method will be called based on the type of object passed in.
We then create instances of the Dog
and Cat
classes and pass them to the animal_sound
function. The appropriate implementation of the make_sound
method is called for each instance, demonstrating polymorphism in action.
In conclusion, polymorphism is a key concept in object-oriented programming that allows for greater flexibility and reusability of code. By allowing objects of different classes to be treated as if they were the same type of object, we can create more modular and extensible code. In Python, we can achieve polymorphism through method overriding and method overloading.