Polymorphic problem in Java
Polymorphism is the perception of executing a single action in different ways. Polymorphism is derived from Greek words: poly and morphs. The word "poly" means many and "morphs" means form. Polymorphism means many forms.
There are two types of polymorphism in Java :compile time polymorphism and run time polymorphism. It can be carred out by using method overloading and overriding concept. Method overriding is resolved at compile time by invoking those methods within the subclass using super keyword as has been discussing previous section. This section will discuss the overloading and overriding methods using static and dynamic binding.
Example :lets take a example, polymorphic problem in Java, normally we can't create objects of interface, but in polymorphic problem we can solve this problem because we can declare refrance of interface and declare that object of other class, that can be possible only polymorphic problem in Java. That can be follow as.
interface A
{
void show(int k );
}
class students implements A
{
public void show(int k )
{
System.out.println(k);
}
}
class Test implements A
{
public void show(int k)
{
System.out.println(k*k);
}
}
class Demo
{
public static void main(String args[])
{
A ob;
ob=new Students();
ob.show(7);
ob=new Test();
ob.show(7);
}
}
Explanation :
As you see in the program, we can declare two classes Test and student and a interface name is A. It can be implemented in the both classes student and test. We can declare refrance of interface A and declare object of Student class. And passing the parameter.