0% found this document useful (0 votes)
25 views6 pages

Maulana Abul Kalam Azad University of Technology, West Bengal

This technical report explores Java polymorphism, a key concept in object-oriented programming that allows objects to be treated as instances of their parent class. It covers the two main types of polymorphism—compile-time (static) and runtime (dynamic)—along with their implementations and benefits, such as code reusability and flexibility. The report also discusses practical applications of polymorphism in frameworks, design patterns, and GUI development.

Uploaded by

Ranju samanta
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)
25 views6 pages

Maulana Abul Kalam Azad University of Technology, West Bengal

This technical report explores Java polymorphism, a key concept in object-oriented programming that allows objects to be treated as instances of their parent class. It covers the two main types of polymorphism—compile-time (static) and runtime (dynamic)—along with their implementations and benefits, such as code reusability and flexibility. The report also discusses practical applications of polymorphism in frameworks, design patterns, and GUI development.

Uploaded by

Ranju samanta
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/ 6

Maulana Abul Kalam Azad University

of Technology, West Bengal

NAME: - RANJU SAMANTA


REG. NO: - 231180120245 OF 2023-24
ROLL. NO: - 11801623047
DEPARTMENT: – ELECTRICAL ENGINEERING
YEAR: - 3 rd (5 th SEMESTER)
INSTITUTE: – BIRBHUM INSTITUTE OF ENGINEERING &
TECHNOLOGY
SUBJECT: – OBJECT ORIENTED PROGRAMMING

SUBJECT CODE: -OE-EE-501B


TOPIC: -TECHNICAL REPORT ON JAVA POLYMORPHISM
Technical report on Java Polymorphism
Abstract
Polymorphism is a core concept in object-oriented programming (OOP)
that allows objects to be treated as instances of their parent class rather
than their actual class. This report provides an in-depth exploration of
polymorphism in Java, covering its types, implementation, and practical
applications. It is aimed at developers, engineers, and computer science
students who seek a comprehensive understanding of how polymorphism
enhances code flexibility and reusability.
Introduction
Polymorphism, derived from the Greek words "poly" (many) and "morph"
(form), enables objects to be treated in multiple forms. In Java,
polymorphism allows methods to perform different tasks based on the
object that invokes them, leading to more flexible and reusable code. Java
achieves polymorphism through method overriding and method
overloading, both of which play a significant role in enhancing the
dynamism of the code.
Types of Polymorphism
Java supports two main types of polymorphism:
1. Compile-time Polymorphism (Static Polymorphism)
2. Runtime Polymorphism (Dynamic Polymorphism)
Compile-time Polymorphism
Compile-time polymorphism, or static polymorphism, is achieved through
method overloading. Method overloading occurs when multiple methods
in a class have the same name but different parameter lists (different types
or numbers of parameters).
Example of Method Overloading:
class MathOpera ons {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}

// Method to add three integers


public int add(int a, int b, int c) {
return a + b + c;
}

// Method to add two double values


public double add(double a, double b) {
return a + b;
}
}

public class Main {


public sta c void main(String[] args) {
MathOpera ons math = new MathOpera ons();
System.out.println(math.add(5, 10)); // Calls add(int, int)
System.out.println(math.add(5, 10, 15)); // Calls add(int, int, int)
System.out.println(math.add(5.5, 10.5)); // Calls add(double,
double)
}
}

In the above example, the add method is overloaded with different


parameter lists, demonstra ng compile- me polymorphism.
Run me Polymorphism: -

Run me polymorphism, or dynamic polymorphism, is achieved through


method overriding. Method overriding occurs when a subclass provides a
specific implementa on for a method that is already defined in its superclass. The
method in the superclass must be marked as virtual (implicitly in Java) and be
overridden in the subclass using the @Override annota on.

Example of Method Overriding: -

class Animal {
// Method to make sound
public void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
public void makeSound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


@Override
public void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public sta c void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();

myDog.makeSound(); // Output: Dog barks


myCat.makeSound(); // Output: Cat meows
}
}

In this example, the makeSound method is overridden in both the Dog and Cat
classes. At run me, the JVM determines which method implementa on to call
based on the actual object type, demonstra ng run me polymorphism.

Benefits of Polymorphism
1. Code Reusability: Polymorphism enables code reusability by
allowing a single interface to be used for different underlying forms
(data types). This reduces redundancy and increases maintainability.
2. Flexibility: It provides flexibility by allowing methods to be used
interchangeably. For instance, you can write methods that work with
the base class type but operate on objects of derived classes.
3. Ease of Maintenance: Code changes can be localized to subclasses
without affec ng the base class. This makes it easier to update or
extend func onality.
4. Dynamic Binding: Run me polymorphism allows for dynamic
method resolu on, which means that the method that gets executed
is determined at run me, providing greater flexibility in method
execu on.
Prac cal Applica ons
1. Frameworks and Libraries: Java frameworks like Spring and
Hibernate extensively use polymorphism to provide flexible APIs that
can handle various object types in a unified manner.
2. Design Pa erns: Many design pa erns, such as the Strategy pa ern
and Factory pa ern, rely on polymorphism to encapsulate behavior
and promote code flexibility.
3. GUI Development: In GUI applica ons, polymorphism allows for the crea on
of generic event handlers that can handle different types of events and
components.
Conclusion
Polymorphism is a fundamental concept in Java that enhances the
flexibility, reusability, and maintainability of code. By leveraging both
compile- me and run me polymorphism, developers can write more
adaptable and efficient so ware. Understanding and u lizing
polymorphism effec vely is crucial for designing robust and scalable
applica ons in Java.

THANK YOU

You might also like