Polymorph Is M
Polymorph Is M
class MathOperations:
return a + b
return a + b + c
return a + b + c + d
Method Overriding
Method Overriding is another crucial concept in OOP) that empowers
subclasses to provide a specific implementation for a method already defined
in their superclass. In Python, this powerful feature allows developers to tailor
the behaviour of a method in a subclass to suit the unique requirements of
that subclass.
class Shape:
def area(self):
pass
class Square(Shape):
self.side = side
def area(self):
Abstraction in Python
Abstraction is used to hide the internal functionality of the function from the
users. The users only interact with the basic implementation of the function,
but inner working is hidden. User is familiar with that "what function
does" but they don't know "how it does."
A class that consists of one or more abstract method is called the abstract
class.
An abstract class is also helpful to provide the standard interface for different
implementations of components.
An Abstract class can contain the both method normal and abstract method.
Python provides the abc module to use the abstraction in the Python
program.
Syntax
class ClassName(ABC):
Example:
class Demo(ABC):
def write(self):
print("Hello....")
@abstractmethod
def display(self):
pass
class DerivedDemo(Demo):
def display(self):
#Driver Code
d=DerivedDemo()
d.write()
d.display()
OUTPUT:
Hello....
In Derived class defining Abstract Method
Interfaces:
An abstract class and interface appear similar in Python.
The only difference in two is that the abstract class may have some non-abstract
methods, while all methods in interface must be abstract, and the implementing
class must override all the abstract methods.
Formal Interface
Informal Interface
Formal Interface
Formal interfaces in Python are implemented using abstract base class
(ABC). To use this class, you need to import it from the abc module.
Example
In this example, we are creating a formal interface with two abstract
methods.
Informal Interface
In Python, the informal interface refers to a class with methods that can
be overridden. However, the compiler cannot strictly enforce the
implementation of all the provided methods.
This type of interface works on the principle of duck typing. It allows us to
call any method on an object without checking its type, as long as the
method exists.
Example
In the below example, we are demonstrating the concept of informal
interface.
Metaclasses
are a powerful feature in Python that allow you to customize class creation.
By using metaclasses, you can add specific behaviors, attributes, and methods
to classes, and allowing you to create more flexible, efficient programs.
This classes provides the ability to work with metaprogramming in Python.
Python provides the functionality to create custom metaclasses by using the
keyword type.
Type is a metaclass whose instances are classes.
Any class created in python is an instance of type metaclass.
Python uses type() function to construct the metaclasses.
Creating Metaclasses Dynamically
The type() function in Python can be used to create classes metaclasses
dynamically.
Customizing Metaclass Creation
In Python, you can customize how classes are created and initialized by
defining your own metaclass. This customization is useful for various
metaprogramming tasks, such as adding specific behavior to all instances of
a class or enforcing certain patterns across multiple classes.
type(classname,bases,attributes)
type(‘Demo’,(),{})
type(‘Demo’,(Base,),{‘name’:’Anurag’})
class Demo(Base):
name=’Anurag’