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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
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 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
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