Griff's answer is spot on but here's some code too:
class A
{
public void output() { System.out.println("Called A."); }
}
class B extends A
{
@Override
public void output() { System.out.println("Called B."); }
public void somethingSpecificToB() { System.out.println("B only."); }
}
class C extends A
{
public void somethingSpecificToC() { System.out.println("C only."); }
}
class Test
{
public static void main(String[] args)
{
polymorphicParam(new A());
polymorphicParam(new B());
polymorphicParam(new C());
A value = polymorphicReturn(true);
value.output();
value = polymorphicReturn(false);
value.output();
}
private static void polymorphicParam(A param)
{
param.output();
}
private static A polymorphicReturn(bool createA)
{
if (createA)
return new A();
return new B();
}
}
So here, class B and C can both still satisfy calls to
output()
so therefore can be "up-cast" and treated like instances of class A. Class B uses its own implementation of
output()
while class C inherits class A's implementation.
In fact, because Java forces either overriding or inheriting public members from super-classes, this sub-type polymorphism always works. Java won't let you break it by, for example, changing
output()
to
private
in a subclass.