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

Back to Question 7

Answer 6

C. Base

Explanation 6

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

Back to Question 6

Answer 5

A. Compiler Error

Explanation 5

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:

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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