<< Chapter < Page | Chapter >> Page > |
Base myVar = new Base();
((A)myVar).test();
Could call test directly on the reference
Having done this, the program could call the method named test directly on the reference variable using a statement such as the following,which is not part of this program.
myVar.test();
This statement would cause the version of the method named test defined in the class named Base to be called, causing the word Base to appear on the standard output device.
This downcast is not allowed
However, this program attempts to cause the version of the method named test defined in the class named A to be called, by downcasting the reference to type A before calling the method named test . This is shown in the following fragment.
((A)myVar).test();
A runtime error occurs
This program compiles successfully. However, the downcast shown above causes the following runtime error to occur under JDK 1.3:
Exception in thread "main" java.lang.ClassCastException: Base
at Worker.doIt(Ap126.java:22)at Ap126.main(Ap126.java:15)
What you can do
You can store an object's reference in a reference variable whose type is a superclass of the class from which the object was originally instantiated.Later, you can downcast the reference back to the type (class) from which the object was instantiated.
What you cannot do
However, you cannot downcast an object's reference to a subclass of the class from which the object was originally instantiated.
Unfortunately, the compiler is unable to detect an error of this type. The error doesn't become apparent until the exception is thrown at runtime.
C. Base
Totally straightforward code
This rather straightforward program instantiates an object of the class named Base and assigns that object's reference to a reference variable of the type Base as shown in the following fragment .
Base myVar = new Base();
myVar.test();
Then it calls the method named test on the reference variable.
Class Base defines the method named test
The class named Base contains a concrete definition of the method named test as shown in the following fragment. This is the method that is called by the code shown in the above fragment .
class Base{
public void test(){System.out.print("Base ");};
}//end class Base
Class A is just a smokescreen
The fact that the class named A extends the class named Base , and overrides the method named test , as shown in the following fragment, is of absolutely no consequence in the behavior of this program. Hopefully youunderstand why this is so. If not, then you still have a great deal of studying to do on Java inheritance.
class A extends Base{
public void test(){System.out.print("A ");
}//end test()}//end class A
A. Compiler Error
Cannot instantiate an abstract class
This program defines an abstract class named Base . Then it violates one of the rules regarding abstract classes, by attempting to instantiate an object of the abstract class as shown in the following code fragment.
Base myVar = new Base();
The program produces the following compiler error under JDK 1.3:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?