In: Computer Science
There are two kinds of interface polymorphism. Explain each with the use of Java snippets to show polymorphic behavior. (Your code doesn't have to implement any algorithmic logic, just the essential set up needed to support your explanation.)
Static polymorphism
public class StaticPolymorphism {
public static int sum(int a,int b) {
return a+b;
}
public static int sum(int a,int b,int c) {
return a+b+c;
}
public static void main(String[] args) {
System.out.println(sum(10,20));
System.out.println(sum(10,20,25));
}
}
Dynamic polymorphism
interface Shape{
double area();
}
class Circle implements Shape{
private double radius;
public Circle(double aRadius) {
super();
radius = aRadius;
}
@Override
public double area() {
return Math.PI * radius *
radius;
}
}
public class DynamicPolymorphism {
public static void main(String[] args) {
Shape s = new Circle(10);
System.out.println(s.area());
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME