What is polymorphism in Java 1 It is when a class has severa

What is polymorphism in Java?

1. It is when a class has several methods with the same name but different parameter types

Solution

Polymorphism in JAVA is a way through which a single action can be performed in multiple ways. Hence, in the given options:

1. This is the correct option as in this a class is having several methods and all the methods have the same name but different parameter types. All of the methods will be performing the same operation but depending upon the parameter value will have different results.

For eg:

class poly
{
   public static void main(String args[]){
B b = new B();//creating an object for class B
   b.add(4,5);//calling add function of class A
   b.add(1,2,3);//calling add function of class B
}
}
class A
{
void add(int a, int b)
{
   int sum=a+b;//adding two numbers
   System.out.println(\"Sum of two numbers=\"+sum);
}
}
//class B is extending the properties of class A
class B extends A
{
void add(int a, int b,int c)
{
   int sum=a+b+c;//adding three numbers
   System.out.println(\"Sum of three numbers=\"+sum);
}
}

What is polymorphism in Java? 1. It is when a class has several methods with the same name but different parameter types SolutionPolymorphism in JAVA is a way t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site