<< 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{

Back to Question 3

Answer 2

C. A

Explanation 2

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.

Back to Question 2

Answer 1

A. Compiler Error

Explanation 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 , 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();

Back to Question 1

-end-

Questions & Answers

the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask