Polymorphism in Java
Polymorphism in Java
1
Method overloading in Java is also known as Compile-time
Polymorphism, Static Polymorphism, or Early binding. In Method
overloading compared to the parent argument, the child argument will
get the highest priority.
Example of Method Overloading
// Java program to demonstrate working of method
// overloading in Java
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Output
30
60
31.0
2
1. Changing the Number of Parameters
Method overloading can be achieved by changing the number of
parameters while passing to different methods.
Below is the implementation of the above method:
// Java Program to Illustrate Method Overloading
// By Changing the Number of Parameters
// Class 1
// Helper class
class Product {
// Method 1
// Multiplying two integer values
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}
// Method 2
// Multiplying three integer values
public int multiply(int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside main()
// method
Product ob = new Product();
3
System.out.println(
"Product of the three integer value :" + prod2);
}
}
Output
Product of the two integer value :2
Product of the three integer value :6
// Class 1
// Helper class
class Product {
// Multiplying three integer values
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
class GFG {
public static void main(String[] args)
{
Product obj = new Product();
4
"Product of the three double value :" + prod2);
}
}
Output
Product of the three integer value :6
Product of the three double value :6.0
// Class 1
// Helper class
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " "
+ "Roll-No :" + roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " "
+ "Name :" + name);
}
}
// Class 2
// Main class
class GFG {
// Main function
public static void main(String[] args)
{
// Creating object of above class
Student obj = new Student();
5
// Passing name and id
// Note: Reversing order
obj.StudentId("Spyd3r", 1);
obj.StudentId(2, "Kamlesh");
}
}
2. Runtime Polymorphism
Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a
process in which a function call to the overridden method is resolved at Runtime.
This type of polymorphism is achieved by Method Overriding. Method overriding,
on the other hand, occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
Method Overriding
6
}
Example
// Java Program for Method Overriding
// Class 1
// Helper class
class Parent {
// Class 2
// Helper class
class subclass1 extends Parent {
// Method
void Print() {
System.out.println("subclass1");
}
}
// Class 3
// Helper class
class subclass2 extends Parent {
// Method
void Print() {
System.out.println("subclass2");
}
}
// Class 4
// Main class
class Geeks {
7
a = new subclass2();
a.Print();
}
}
Output
subclass1
subclass2
Example
// Example of Overriding in Java
class Animal {
// Base class
void move() { System.out.println(
"Animal is moving."); }
void eat() { System.out.println(
"Animal is eating."); }
}
Output
Dog is running.
Animal is eating.
Dog is barking.
Explnation: The Animal class defines base functionalities like move() and eat().t he Dog
class inherits from Animal and overrides the move() method to provide a specific
8
behavior Dog is running. Both classes can access their own methods. When creating a Dog
object, calling move() executes the overridden method.