0% found this document useful (0 votes)
7 views13 pages

Polymorph Is M

The document explains key Object-Oriented Programming (OOP) concepts in Python, including polymorphism, method overloading, method overriding, and access modifiers (public, protected, private). It also discusses abstraction, interfaces, and metaclasses, highlighting how these features enable cleaner code and flexible design. Additionally, it provides examples to illustrate these concepts and their implementation in Python.

Uploaded by

deepthi.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Polymorph Is M

The document explains key Object-Oriented Programming (OOP) concepts in Python, including polymorphism, method overloading, method overriding, and access modifiers (public, protected, private). It also discusses abstraction, interfaces, and metaclasses, highlighting how these features enable cleaner code and flexible design. Additionally, it provides examples to illustrate these concepts and their implementation in Python.

Uploaded by

deepthi.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Polymorphism

Means having many forms. In programming, polymorphism means the same


function name (but different signatures) being used for different types. The
key difference is the data types and number of arguments used in function.
What is Method Overloading?
Method Overloading is a fundamental concept in OOP that enables a class to
define multiple methods with the same name but different parameters.
In Python, this powerful feature allows developers to create versatile
functions capable of handling various data types. It also helps perform distinct
operations based on the types and number of arguments passed.

When a method is overloaded, Python determines which version of the


method is to be executed based on the arguments provided during the
function call. By using Method Overloading, programmers can create cleaner
and more concise code. It is because related functionalities can be grouped
under the same method name.

class MathOperations:

def add(self, a, b):

return a + b

def add(self, a, b, c):

return a + b + c

def add(self, a, b,c, d):

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.

When a method is overridden, the version defined in the subclass takes


precedence over the one in the superclass.

Method Overriding is particularly useful in scenarios where a subclass needs


to enhance or modify the functionality inherited from its superclass while
maintaining the overall structure and interface.

class Shape:

def area(self):

pass

class Square(Shape):

def __init__(self, side):

self.side = side

def area(self):

return self.side * self.side

Public Access Modifier:


The members of a class that are declared public are easily accessible from
any part of the program. All data members and member functions of a class
are public by default.

Protected Access Modifier:


The members of a class that are declared protected are only accessible to a
class derived from it. Data members of a class are declared protected by
adding a single underscore ‘_’ symbol before the data member of that class.

Private Access Modifier:


The members of a class that are declared private are accessible within the
class only, private access modifier is the most secure access modifier. Data
members of a class are declared private by adding a double underscore ‘__’
symbol before the data member of that class.

def add(self):---add() is public

def __add(self):---add() is private

def _add(self):---add() is protected

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."

Abstraction is used to hide the irrelevant data/class in order to reduce the


complexity.
Abstraction can be achieved by using abstract classes and interfaces.

A class that consists of one or more abstract method is called the abstract
class.

Abstract methods do not contain their implementation.


Abstract class can be inherited by the subclass and abstract method gets its
definition in the subclass.

Abstraction classes are meant to be the blueprint of the other class.

An abstract class can be useful when we are designing large functions.

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.

An Abstract class cannot be instantiated; we cannot create objects for the


abstract class.

Python provides the abc module to use the abstraction in the Python
program.

Let's see the following syntax.

Syntax

from abc import ABC

class ClassName(ABC):

We import the ABC class from the abc module.

Example:

from abc import ABC,abstractmethod

class Demo(ABC):

def write(self):

print("Hello....")

@abstractmethod

def display(self):
pass

class DerivedDemo(Demo):

def display(self):

print("In Derived class defining Abstract Method ")

#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.

Rules for implementing Python Interfaces:


 Methods defined inside an interface must be abstract.
 Creating object of an interface is not allowed.
 A class implementing an interface needs to define all the methods of
that interface.
 In case, a class is not implementing all the methods defined inside the
interface, the class must be declared abstract.

Ways to implement Interfaces in Python


We can create and implement interfaces in two ways −

 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.

Let us provide a class that implements both


the abstract methods.
Output
This is method1
This is method2

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.

Customizing the classes can be done by overriding methods in the


metaclass, specifically __new__ and __init__.
Classes creted dyna….

type(classname,bases,attributes)
type(‘Demo’,(),{})
type(‘Demo’,(Base,),{‘name’:’Anurag’})

class Demo(Base):
name=’Anurag’

You might also like