[Go to site: main page, start]

0% found this document useful (0 votes)
5 views3 pages

Java Polymorphism Explained: OOP Concepts

Polymorphism in Java allows methods or object references to behave differently based on context, enhancing flexibility and maintainability. It includes compile-time polymorphism through method overloading and runtime polymorphism through method overriding. Additionally, interfaces support polymorphism by enabling multiple implementations, promoting loosely coupled code.

Uploaded by

akuthotaabhinaya
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)
5 views3 pages

Java Polymorphism Explained: OOP Concepts

Polymorphism in Java allows methods or object references to behave differently based on context, enhancing flexibility and maintainability. It includes compile-time polymorphism through method overloading and runtime polymorphism through method overriding. Additionally, interfaces support polymorphism by enabling multiple implementations, promoting loosely coupled code.

Uploaded by

akuthotaabhinaya
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

Java OOPS – Polymorphism (Detailed

Notes)
What is Polymorphism?
Polymorphism means one name, many forms. In Java it allows the same method or object
reference to behave differently based on context. It improves flexibility, scalability and
maintainability. There are two types: compile-time (method overloading) and runtime
(method overriding). Polymorphism allows us to write generic code that works for multiple
object types. This reduces coupling and improves extensibility.

Example Code:
class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Dog barks"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}

Types of Polymorphism
Compile-time polymorphism is achieved through method overloading. Runtime
polymorphism is achieved through method overriding and dynamic method dispatch.
Overloading resolves method calls at compile time, while overriding resolves them at
runtime based on object type.

Example Code:
class Calc {
int add(int a,int b){return a+b;}
double add(double a,double b){return a+b;}
}
Method Overloading
Method overloading means defining multiple methods with the same name but different
parameters. It improves readability and allows similar operations with different inputs.

Example Code:
class MathOp {
int add(int a,int b){return a+b;}
int add(int a,int b,int c){return a+b+c;}
}

Method Overriding
Method overriding occurs when a child class provides its own implementation of a parent
method. It enables runtime polymorphism.

Example Code:
class A {
void show(){[Link]("A");}
}
class B extends A {
void show(){[Link]("B");}
}

Dynamic Method Dispatch


Dynamic method dispatch is the mechanism by which overridden method calls are resolved
at runtime based on the object type, not reference type.

Example Code:
A obj = new B();
[Link]();

Polymorphism with Interfaces


Interfaces support polymorphism by allowing multiple implementations of the same
interface, enabling flexible and loosely coupled code.

Example Code:
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw(){[Link]("Circle");}
}
class Square implements Shape {
public void draw(){[Link]("Square");}
}

You might also like