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

is economics important in programming world?
Abdul Reply
What is oppunity cost
Kudzanayi Reply
it refers wants that are left unsatisfied in Oder satisfy another more pressing need
Osei
Thank bro
Kudzanayi
is the second altenative to foregone
swedy
How does monopoly and imperfect competion;public goods externalities ass symmetric information, ommon property ressourses; income distribution Merit goods and Macroeconomic growth and stability causes market failure?
Gcina
it is where by a labour moves from one place to another
Amoako Reply
yh
Osei
another
Amoako
yh
Osei
ok
Amoako
what is mobility of labour
George Reply
who best defined economics
paul Reply
what are the importance of economics
Adwoa Reply
answer
Asamoah
trade should be best
Aakash
help to know how our government operates in which system
swedy
1. To solve economic problems. 2. To predict economic event. 3. It also offer intellectual training to students.
samuel
How price elasticity can affect the markets of certain goods
Rhoda Reply
Heyy thanks for the teaching
Easter Reply
what is labour
Arthur Reply
Mental and physical ability of human being is considered as labour.
JEYARAM
and usually provided by human
KEMAWOR
is all human effort both physical and mental abilities to work
Asamoah
why is it that most countries in Africa abuse available resources
Okeoghene Reply
What is economics
Bright Reply
economics is a science which studies human behaviour as a relationship between ends, scarce means which have alternative uses.
prince
Why is scarcity a mind problem in economics
Alima Reply
because of the problem of allocation of resources
Osola
unequalled distribution of resources
Agyen
thanks
Alima
scarcity is a mind problem due to circumstances like when a particular product is demand in a high rate at the market.
Saihou
scarcity defines limited in supply relative to the demand them. so scarcity is a mind problem in economics because wants are unlimited while resources are also limited.
prince
it is a mind problem because it's one of the fundamental issue address by economics human wants are unlimited and resources available are limited this makes the study of economics essential
rashid
its a fundamental issue
zahid
it's not a mind problem, I think it's a economic problem, how to allocate scarce resources to satisfy need and want of society
Samiullah
Identify the different sectors in the economy
Moses Reply
what is economics
Moses
economic is study of human behaviour according to how they satisfie their numerous want
Osola
Economics is a science which studies human behaviour as a relationship between ends and scares resources.
JEYARAM
economic is study of human behaviour according to how they satisfie their numerous want
Amar
what is the law of demand
Adugbire Reply
the lower the price the higher the quantity demanded vice versa is true
Nadhin
yes
vivek
yes ooh
Asamoah
right
samuel
what are relationship between unemployment and economic growth
Weness Reply
no relationship
Awuah
When there is economic growth, there is increased opportunity in employment. When there is no economic growth mean there is recession causing a decline or downsizing in employment opportunities.
Teescou
unemployment or employment determined by the level of economic growth
Osola
Difference between extinct and extici spicies
Amanpreet Reply
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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