<< Chapter < Page Chapter >> Page >

Accessing private members

In those cases where it is necessary for an object of the local class to access private members of the containing object, the compiler automaticallycreates and uses accessor methods that make such access possible.

Similar to your code

The bottom line is that the code that is automatically produced by the compiler is probably very similar to code that you would write if you werewriting the program using only top-level classes. The good news is that you don't have to write that extra code, and you don't have to maintain it. Theextra code is written for you, and if you modify your class structure, the extra code is automatically modified accordingly.

Discussion and sample code

The paragraphs that follow will explain a program named InnerClasses07 . This program is designed specifically to illustrate various characteristics of localclasses. I will discuss the program in fragments. A complete listing is shown in Listing 17 near the end of the module.

This program illustrates the use of local classes. The program consists of a total of five classes:

  • Four top-level classes named
    • InnerClasses07
    • A
    • X
    • Y
  • A local class named B.

When compiled, the program produces the class files shown in Figure 1 . The file named A$1$B.class represents the local class. The remaining files in Figure 1 represent the four top-level classes.

Class hierarchy

In the previous module, I explained that once you understand the class file naming convention, you can determine from the filenames how top-level classes and member classes are structured. However, the situation isn't nearly so clear when it comes to local classes and anonymousclasses. This will become more apparent in the next module, which combines local classes and anonymous classes.

Overall program structure and behavior

Containment hierarchy

The program named InnerClasses07 defines a local class named B inside an instance method named meth .

The method named meth is an instance method of a top-level class named A . The method named meth instantiates two separate objects of the local class named B , and calls a method named showB on each of them.

The method named showB displays certain data values that illustrate the characteristics of localclasses, as well as the containment hierarchy among objects of the local class and an object of the containing class.

In this case, objects of the class named B are contained within an object of the class named A . (The class named A is an enclosing class of the class named B .)

Inheritance hierarchy

The top-level class named A extends the top-level class named X , (which in turn, extends Object ).

The class named B is contained in or enclosed by the top-level class named A , but extends the top-level class named Y , (which in turn, extends Object ).

There is no inheritance relationship between the classes X and Y (aside from their common superclass named Object ).

There is no inheritance relationship between the classes A and B .

The method named showB also displays data values that are intended to demonstrate that the inheritance hierarchy is independent of thecontainment hierarchy.

Questions & Answers

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
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
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