Use a reference variable to refer to any object that is a subclass of the declared reference variable type : Reference Type Casting « Type Casting « SCJP
4.3.16.Use a reference variable to refer to any object that is a subclass of the declared reference variable type
class Foo { public void doFooStuff() { }
} class Bar extends Foo { public void doBarStuff() { }
} class MainClass { public static void main (String [] args) {
Foo reallyABar = new Bar(); // Legal because Bar is a
// subclass of Foo
Bar reallyAFoo = new Foo(); // Illegal! Foo is not a
// subclass of Bar
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Foo to Bar
at MainClass.main(MainClass.java:11)