<< Chapter < Page | Chapter >> Page > |
An abstract method doesn't have a body.
Abstract classes and methods
A class that contains an abstract method must itself be declared abstract . However, an abstract class is not required to contain abstract methods.
Failed to declare the class abstract
In this program, the class named Base contains an abstract method named test , but the class is not declared abstract as required.
class Base{
abstract public void test();}//end class Base
Therefore, the program produces the following compiler error under JDK 1.3:
Ap122.java:24: Base should be declared abstract;it does not define test in Base
class Base{
C. A
If you missed this ...
If you missed this question, you didn't pay attention to the explanation for Question 1 .
Define a method in a subclass
This program defines a subclass named A that extends a superclass named Base . A method named test is defined in the subclass named A but is not defined in any superclass of the class named A .
Store a reference as a superclass type
The program declares a reference variable of the superclass type, and stores a reference to an object of the subclass in that reference variable as shown inthe following code fragment.
Base myVar = new A();
Downcast and call the method
Then the program calls the method named test on the reference stored as the superclass type, as shown in the following fragment.
((A)myVar).test();
Unlike the program in Question 1 , the reference is downcast to the true type of the object before calling the method named test . As a result, this program does not produce a compiler error.
Why is the cast required?
As explained in Question 1 , it is allowable to store a reference to a subclass object in a variable of a superclass type. Also,as explained in Question 1 , it is not allowable to directly call, on that superclass reference, a method of the subclass objectthat is not defined in or inherited into the superclass.
However, such a call is allowable if the programmer purposely downcasts the reference to the true type of the object before calling the method.
A. Compiler Error
Define a method in a subclass
This program defines a subclass named A that extends a superclass named Base . A method named test , is defined in the subclass named A , which is not defined in any superclass of the class named A .
Store a reference as superclass type
The program declares a reference variable of the superclass type, and stores a reference to an object of the subclass in that reference variable as shown inthe following code fragment.
Base myVar = new A();
Note that no cast is required to store a reference to a subclass object in a reference variable of a superclass type. The required type conversion happensautomatically in this case.
Call a method on the reference
Then the program attempts to call the method named test on the reference stored as the superclass type, as shown in the following fragment.This produces a compiler error.
myVar.test();
The reason for the error
It is allowable to store a reference to a subclass object in a variable of a superclass type. However, it is not allowable to directly call, (on that superclass reference) , a method of the subclass object that is not defined in or inherited into the superclass.
The following error message is produced by JDK 1.3.
Ap120.java:18: cannot resolve symbol
symbol : method test ()location: class Base
myVar.test();
The solution is ...
This error can be avoided by casting the reference to type A before calling the method as shown below:
((A)myVar).test();
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?