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

summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
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