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

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Type Casting » Reference Type Casting 
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)
4.3.Reference Type Casting
4.3.1.Object reference conversion
4.3.2.An interface type can be converted to an interface type or to Object.
4.3.3.Convert interface to super-interface
4.3.4.Object Method-Call Conversion
4.3.5.Cast between an interface and a nonfinal object.
4.3.6.Runtime casting: the class being converted must be itself or must inherit from it.
4.3.7.If New type is an interface, the class of the expression being converted must implement New type.
4.3.8.For = with object references, if the type of the left operand is a class C, then the type of the right operand must be a subclass of C or the null value.
4.3.9.If the type of the left operand is an interface I, the type of the right operand must be a subinterface of I, or a class that implements I, or the null value.
4.3.10.A reference to any object can be cast into a reference to an object of class Object.
4.3.11.A reference to an object can be cast into a reference to an object of its parent class.
4.3.12.A reference to an object can be cast into a reference to an object of its implemented interface
4.3.13.A reference to an object can be cast into a reference to an object of its implemented super interface
4.3.14.Casting Object References with Collections
4.3.15.An Object reference can be converted to:
4.3.16.Use a reference variable to refer to any object that is a subclass of the declared reference variable type
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.