0% found this document useful (0 votes)
7 views

OE-EE501B

Uploaded by

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

OE-EE501B

Uploaded by

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

Technical Report Writing

On

Polymorphism in Object Oriented Programming

Submitted by
Name: Anushree Oraon
Department: Electrical Engineering
Semester: 5th
Roll Number: 16901622011
Paper Name: Object Oriented Programming
Paper Code: OE-EE-501B

Department of Electrical Engineering


Academy of Technology
Aedconagar, Hooghly-712121
West Bengal, India
ABSTRACT
Object-oriented programming (OOP) is a cornerstone of modern software
development, offering powerful tools and principles for building complex
and maintainable software systems. Among the foundational concepts of
OOP, polymorphism stands out as a central and essential concept.
Polymorphism is the capability that allows objects of different classes to
be treated as objects of a common superclass. It is not merely a
theoretical construct but a practical and versatile tool that profoundly
impacts code design and reusability.
This technical report delves into the multifaceted world of polymorphism,
categorizing its various types and exploring their real-world applications.
It specifically analyzes how polymorphism contributes to code reusability,
which is a crucial goal in software engineering. Code reusability reduces
redundancy, minimizes development effort, and facilitates the evolution
of software systems.
INTRODUCTION
Object-oriented programming (OOP) is a widely adopted paradigm for
designing and building software systems. At its core, OOP is founded on
the principles of encapsulation, inheritance, and polymorphism. Among
these, polymorphism stands out as a fundamental concept that plays a
pivotal role in achieving code reusability and flexibility in OOP.
Polymorphism, derived from the Greek words "poly" (many) and
"morphos" (forms), represents the ability of objects of different classes to
be treated as objects of a common superclass. This seemingly abstract
concept has profound implications for how software systems are
designed, maintained, and extended.
In essence, polymorphism empowers developers to write code that is
more adaptable to change, less prone to errors, and easier to maintain. It
does so by allowing different objects to respond to the same method or
operation in a way that is contextually appropriate for their specific types.

This ability to interchange objects seamlessly within a shared interface is


the cornerstone of polymorphism and greatly contributes to the
overarching goal of achieving code reusability.
DISCUSSION
Types of Polymorphism
Compile-time Polymorphism (Static Binding)
Compile-time polymorphism, also known as static binding, is primarily
achieved through method overloading. This is when multiple methods in
the same class share the same name but differ in the number or types of
their parameters. The appropriate method to execute is determined at
compile-time based on the method signature, specifically the number and
types of arguments.

In this example, the MathOperations class has two add methods, one for
integers and another for doubles. The correct method is selected at
compile-time based on the argument types. This helps in code reusability
as you can use the same method name for similar operations but with
different data types.

Runtime Polymorphism (Dynamic Binding)


Runtime polymorphism occurs during program execution and is achieved
through method overriding. It relies on inheritance, where a subclass
provides a specific implementation for a method that is already defined in
its superclass. The actual method to be called is determined at runtime
based on the object's actual (dynamic) type.
In this case, if you have a reference to an Animal object that actually
points to a Dog object, calling makeSound() will execute the Dog class's
implementation. This dynamic binding allows for code reusability as you
can create generic code that works with various subclasses without
knowing their specific implementations.

Ad-hoc Polymorphism
Ad-hoc polymorphism, also known as operator overloading, is a way to
achieve polymorphism based on the types of operands used with
operators. For example, in many programming languages, you can use the
+ operator to add numbers and also concatenate strings. The behavior of
the operator depends on the types of operands involved.

In this example, the + operator exhibits different behavior depending on


the operand types. This form of polymorphism enhances code reusability
as operators can work with various data types, allowing you to write
more generic and reusable code.
Parametric Polymorphism (Generics)
Parametric polymorphism is commonly referred to as generics in
programming languages like Java and C#. It enables developers to write
code that operates on different data types without specifying those types
explicitly. This is particularly useful for creating reusable data structures
and algorithms.

In this example, the Box class uses a generic type T to hold values of any
data type. This promotes code reusability as you can create a single
generic class that can be used with different data types, reducing the
need for code duplication.

Polymorphism's Usage in Code Reusability


Polymorphism greatly enhances code reusability in several ways:

Interface-Based Polymorphism
Interfaces in OOP define a contract that classes must implement. By
programming to interfaces rather than concrete classes, you can create
code that's more adaptable to changes. New classes can easily replace old
ones as long as they adhere to the same interface, enhancing code
reusability.
Here, the Drawable interface ensures that any class implementing it will
have a draw method. This allows you to create code that can work with
any Drawable object, promoting code reusability and extensibility.

Inheritance and Method Overriding


Inheritance allows classes to inherit properties and behaviors from their
parent classes. Method overriding allows subclasses to provide
specialized implementations while retaining the superclass's interface.
This facilitates the reuse of code across different classes while allowing
for customization where needed.

In this example, the Shape class provides a common interface through the
draw method, which is overridden by Circle and Square. This promotes
code reusability as you can work with Shape objects but still have specific
behavior for each subclass.
Dynamic Dispatch
Runtime polymorphism, achieved through dynamic dispatch, enables you
to write code that can work with objects of different subclasses in a
seamless manner. This promotes code reusability as you can write generic
code that can handle various specific cases.

In this case, even though the references are of type Shape, the
appropriate draw method is dynamically dispatched based on the actual
object's type. This dynamic behavior promotes code reusability as you can
write code that works with different subclasses without modification.
CONCLUSION
Polymorphism is a fundamental concept in OOP that categorizes into
compile-time, runtime, ad-hoc, and parametric polymorphism. Its usage
in code reusability is evident through interface-based polymorphism,
inheritance, dynamic dispatch, and generics. By leveraging polymorphism,
developers can create more flexible, adaptable, and maintainable code,
thus enhancing the overall quality of software systems.
Incorporating polymorphism into software design is essential for
achieving robustness, scalability, and ease of maintenance, making it a
cornerstone of modern object-oriented programming.
REFERENCES

1. Dynamic Programming, by E. V. Denardo, Dover Publications,


2003.
2. “The construction of optimal binary search trees using dynamic
programming is described in The Art of Programming: Sorting
and Searching”, Vol.3, by D. E. Knuth, Second Edition, Addison
Wesley, 1998.

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