<< Chapter < Page Chapter >> Page >

This program illustrates the use of member classes. The program consists of a total of six classes:

  • Top-level classes named
    • InnerClasses06
    • A
    • X
  • Member classes named
    • B
    • C
    • D

When compiled, the program produces the class files shown in Figure 1 .

Class containment hierarchy

Once you understand the class file naming convention, you can determine from the file names in Figure 1 that class B is a member class of class A.

(The class file named A$B.class indicates that the class named B is a member of the class named A.)

Similarly, class C is a member of class B, and class D is a private member of class C.

(However, you cannot tell from the class file names that class D is private.)

Behavior of the program

An object is instantiated from the class named A. This makes it possible to instantiate an object ofthe member class named B. The object of the class named B is internally linked to the object of the class named A.

(This causes the instance variable, constructor parameter, and accessor methods discussed above to be automatically created to link the object ofthe class named B to the object of the class named A.)

The object of the class named B is used to instantiate an object of the member class named C. This object of the class C is linked to the object ofthe class named B.

Instantiate additional objects of classes A and B, plus an object of class D

When the object of the class named C is instantiated, the constructor for that class instantiates separate objects of the classes named Aand B, and also instantiates an object of the private member class named D.

(We will see later that the new and separate object of the class named B continues to be internally linked to the original object of the Class namedA, and is not internally linked to the new object of the class named A.)

Instantiation of the object of class D illustrates the use of private member classes.

(Note that while top-level classes cannot be private, member classes can be private.)

Perform a variety of operations

A variety of operations are performed from within the methods belonging to the object of the class C to illustrate theattributes and behavior of objects instantiated from member classes.

Comments in the code explain the purpose of each of those operations.

Many of those operations produce screen output, which will be shown in conjunction with thecode that produced the output.

Structure of the program

The main method

The main method of the controlling class named InnerClasses06 , is shown in Listing 1 .

The code in Listing 1 instantiates an object of the member class named C and calls the method named cShow on that object.

(Note that it is necessary to first instantiate objects of the enclosing classes named A and B before the object of the member class named C can beinstantiated.)

An independent top-level class named X

Listing 2 shows the definition of an independent top-level class named X.

This class will be extended by the class named C, which is a member of the class named B, which is a member of the class named A. This willillustrate that the inheritance hierarchy is independent of the containment hierarchy.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
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
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