0% found this document useful (0 votes)
38 views15 pages

POLYMORPHSIM

National University of Science and Technology Polymorphism allows objects to take on multiple forms. It enables objects from different classes that inherit from the same parent class to be treated uniformly through their common interface. There are two main types of polymorphism: static polymorphism, which is resolved at compile time through function and operator overloading; and dynamic polymorphism, which is resolved at runtime through virtual functions and abstract classes/interfaces. Polymorphism is implemented differently in languages like C++, Java, and Python but enables flexibility and code reuse through a uniform interface for related types of objects.

Uploaded by

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

POLYMORPHSIM

National University of Science and Technology Polymorphism allows objects to take on multiple forms. It enables objects from different classes that inherit from the same parent class to be treated uniformly through their common interface. There are two main types of polymorphism: static polymorphism, which is resolved at compile time through function and operator overloading; and dynamic polymorphism, which is resolved at runtime through virtual functions and abstract classes/interfaces. Polymorphism is implemented differently in languages like C++, Java, and Python but enables flexibility and code reuse through a uniform interface for related types of objects.

Uploaded by

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

National University of Science and Technology

POLYMORPHISM

SOFTWARE DESIGN AND ARCHITECTURE


01 Definition & Concepts

02 Types & explanation

03 Benefits
Presentation
04 Implementation in code
Agenda
05 Real-Life Examples

06 Best Practices

07 Conclusion
What is Polymorphism

• ability to take more than one form


• in object oriented programming ,ability of variable ,function
or object to take on multiple forms

• creating class objects,inherited from same parent class and have


same names but different behaviours

• enables flexibility by allowing different types of object to be


treated uniformly
Static
• Resolved at compile time
• multiple functions with the same name but
different parameters.
• Function Overloading,
• Operator Overloading

Dynamic
• Resolved at runtime
• enables objects to exhibit different behaviors
based on their actual types.
• Virtual Functions
• Interface Polymorphism
Static Polymorphism

Function Overloading
Function overloading allows multiple
functions with the same name but
different parameter lists to coexist
within the same scope.
Static Polymorphism

Operator Overloading
Operator overloading enables custom
definitions for the behavior of
operators when applied to user-
defined types.
Dynamic Polymorphism
Virtual Functions
A virtual function is a member function in a
base class declared with the `virtual` keyword,
allowing it to be overridden in derived classes
to achieve dynamic polymorphism.
Dynamic Polymorphism
Abstract Class
An abstract class is a class that cannot be
instantiated on its own because it contains one
or more pure virtual functions.
Implementation in Different
Course Syllabus

Programming Languages

C++ Java Python


Cpp
#include <iostream> int main() {
// Abstract base class // Create instances of derived classes
class Shape {
public:
Circle circle;
// Pure virtual function Square square;
virtual void draw() const = 0; // Call draw() on each object
}; Shape* shape=&circle;
// Concrete derived class shape->draw();
class Circle : public Shape {
shape=&square;
public:
void draw() const override { shape->draw();
std::cout << "Drawing a circle" << std::endl; return 0;
} }
};
// Concrete derived class
class Square : public Shape {
public:
void draw() const override {
std::cout << "Drawing a square" << std::endl;
}
};
Java
abstract class Shape { public class Main {
abstract void draw(); public static void main(String[] args) {
} Circle circle = new Circle();
class Circle extends Shape {
Square square = new Square();
@Override
void draw() { Shape shape = circle;
System.out.println("Drawing a circle"); shape.draw();
} shape = square;
} shape.draw();
class Square extends Shape { }
@Override }
void draw() {
System.out.println("Drawing a square");
}
}
Java
interface Shape { public class Main {
void draw(); public static void main(String[] args) {
} Circle circle = new Circle();
class Circle implements Shape {
Square square = new Square();
@Override
public void draw() { Shape shape = circle;
System.out.println("Drawing a circle"); shape.draw();
} shape = square;
} shape.draw();
class Square implements Shape { }
@Override }
public void draw() {
System.out.println("Drawing a square");
}
}
Python
from abc import ABC, abstractmethod
# Main function
# Abstract base class def main():
class Shape(ABC): # Create instances of derived classes
# Abstract method circle = Circle()
@abstractmethod square = Square()
def draw(self):
pass # Call draw() on each object
shape = circle
# Concrete derived class for Circle shape.draw()
class Circle(Shape):
def draw(self): shape = square
print("Drawing a circle") shape.draw()

# Concrete derived class for Square # Entry point of the program


class Square(Shape): if __name__ == "__main__":
def draw(self): main()
print("Drawing a square")
Real World Examples of
Polymorphism

control amna
yl daffa ho
Contact Me Email Address
hello@reallygreatsite.com

Phone Number
123-456-7890

Consultation Hours
2 PM to 4 PM, Monday to Friday

Scan the QR code


for the Course Calendar

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy