Tuesday, 10 September 2013

Are these examples of Polymorphism?

Are these examples of Polymorphism?

After browsed some questions about Polymorphism, it seems that
Polymorphism is a general idea in Java, just to make an Object to behave
as if it were an instance of another class, thus the code is more
independent of the concrete class. Given this idea, are the two method
calls in the following main() usages of Polymorphism ?
abstract class A
{
void f() { System.out.println("A.f()"); }
abstract void g();
}
class B extends A
{
void g() { System.out.println("B.g()"); }
}
public class Test
{
public static void main(String[] args)
{
A a = new B();
a.f(); // Is this an usage of Polymorphism ?
a.g(); // ...
}
}
The output is :
A.f();
B.g();

No comments:

Post a Comment