Polymorphism
in Java
W W W . J A V A 2 B L O G . C O M
Content
Introduction
Method Overloading
Method Overriding
Introduction
When one task is performed by different ways is
known as polymorphism. In java, we can achieve
Polymorphism by:
Method Overloading
Method Overriding
Method Overloading
In method overloading, a class has multiple
methods with same name but different parameters.
we can achieve method overloading by changing
parameter types or by changing the number of
parameters. Method overloading increases the
readability of the program.
Method Overloading
class Addition
{
int sum;
void sum(int a, int b)
{
sum=a+b;
System.out.println("Sum is:" + sum);
}
void sum(int a, int b, int c)
{
sum=a+b+c;
System.out.println("Sum is:" + sum);
}
}
Method Overloading
class Demo
{
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10,12);
obj.sum(16, 19,30);
}
}
Method Overriding
In method overriding, super class and sub class
having methods with same name and same
parameters but having different implementations. It is
mainly used to provide the specific implementation of
a method which is already defined in its super class. It
is used to achieve run-time polymorphism.
Method Overriding
class Super
{
void display()
{
System.out.println("I am super class");
}
}
Method Overriding
class Sub extends Super
{
void display()
{
System.out.println("I am sub class");
}
public static void main(String args[])
{
Sub obj=new Sub();
obj.display();
}
}
thank you
www.java2blog.com

Polymorphism in Java