OE-EE501B
OE-EE501B
On
Submitted by
Name: Anushree Oraon
Department: Electrical Engineering
Semester: 5th
Roll Number: 16901622011
Paper Name: Object Oriented Programming
Paper Code: OE-EE-501B
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.
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 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.
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.
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