Abstract Class in Python:
An abstract class is a class that cannot be instantiated on its own and is meant to be inherited by other classes.
- It serves as a blueprint for other classes and can contain abstract methods, which are methods that are declared but contain no implementation.
- They are used when you want to define common behaviors that should be shared by all subclasses, but you don’t want to provide a default implementation in the base class.
Key Points about Abstract Class:
- It cannot be instantiated directly.
- It can contain both abstract methods (methods with no implementation) and non-abstract methods (methods with implementation).
- Any class that inherits from an abstract class must implement all abstract methods to be instantiated.
Syntax:
- In Python, we use the abc (Abstract Base Class) module to define abstract classes and methods.
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
@abstractmethod
def move(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
def move(self):
return "Run"
# This will give an error since Animal is abstract
# animal = Animal()
dog = Dog()
print(dog.sound()) # Output: Bark
print(dog.move()) # Output: Run- The Animal class is abstract and cannot be instantiated directly.
- The sound and move methods are abstract methods and must be implemented in any subclass.
- The Dog class inherits from Animal and provides implementations for the abstract methods.
Why Use Abstract Classes?
- Enforces a contract: An abstract class defines a set of methods that must be created within any subclass, ensuring consistency in the subclass implementations.
- Code Reusability: Abstract classes can define some common behavior while allowing subclasses to override or extend the functionality as needed.
- Organized Code Structure: Abstract classes allow you to group related functionality together and help you better organize your code.
Operator Overloading in Python:
Operator overloading is a feature in Python that allows you to redefine the behavior of operators (such as +, -, *, etc.) for objects of custom classes.
- By overloading operators, you can make them behave in a way that is specific to your class, allowing you to perform operations on objects that make sense for your class’s data.
Key Points about Operator Overloading:
- It allows you to use operators like +, -, *, etc., with objects of your own class.
- You achieve operator overloading by defining special methods (also called magic methods) within your class.
- Python uses magic methods (or dunder methods) to implement operator overloading.
Syntax:
- To overload operators in Python, you need to implement special methods like add, sub, mul, etc., in your class.
Example of Operator Overloading:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __repr__(self):
return f"Point({self.x}, {self.y})"
# Create two Point objects
p1 = Point(2, 3)
p2 = Point(4, 1)
# Use the overloaded + operator
p3 = p1 + p2 # This calls p1.__add__(p2)
print(p3) # Output: Point(6, 4)- The Point class represents a point in a 2D plane.
- The add method is overloaded to allow addition of two Point objects.
- The repr method provides a string representation for the Point object so that when you print it, it looks readable.
- The + operator is now used to add two Point objects, resulting in a new Point object with the sum of their x and y coordinates.
Magic Methods for Operator Overloading:
- add: For + operator
- sub: For – operator
- mul: For * operator
- truediv: For / operator
- floordiv: For // operator
- mod: For % operator
- eq: For == operator
- lt: For < operator
- le: For <= operator
Why Use Operator Overloading?
- Intuitive Code: Allows you to write more natural and readable code, like using + to add two objects of a custom class.
- Expressiveness: Makes custom classes behave like built-in types, enhancing the expressiveness of your code.
- Flexibility: Provides flexibility in defining how objects of custom classes should interact with operators, making your code more flexible and reusable.
