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

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
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