Fundamentals of Computer and It's Applications
Fundamentals of Computer and It's Applications
forms.
What is Polymorphism in Java?
Polymorphism is considered one of the important features of Object-Oriented
Programming. Polymorphism allows us to perform a single action in different
ways. In other words, polymorphism allows you to define one interface and
have multiple implementations. The word “poly” means many and “morphs”
means forms, So it means many forms.
Method Overloading
When there are multiple functions with the same
name but different parameters then these functions
are said to be overloaded. Functions can be
overloaded by changes in the number of arguments
or/and a change in the type of arguments.
// Class 1
// Helper class
class Helper {
return a * b;
// Method 2
return a * b;
}
// Class 2
// Main class
class GFG {
// input as in arguments
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5,
6.3));
Output
8
34.65
// Class 1
// Helper class
class Parent {
void Print()
// Print statement
System.out.println("parent class");
// Class 2
// Helper class
// Method
void Print()
{ System.out.println("subclass1"); }
// Class 3
// Helper class
// Method
void Print()
// Print statement
System.out.println("subclass2");
}
// Class 4
// Main class
class GFG {
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
Output
subclass1
subclass2