Nested Classes Flashcards

1
Q

Within a class, define data and code.

A

Data - The instance variables of the class.
Code - The way in which the instance variables are operated on in methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Static Nested Class? How are they instantiated?

A

A nested class that can’t access the non-static instances variables of the encapsulating class unless through a reference of the encapsulating class.
They are instantiated in a way such as A.B b = new A.B();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Non-Static Nested Class? How does this class access members of the encapsulating class?

A

Known as an inner class. This class has access to all other members of the encapsulating class and can refer to them directly. It is instantiated through an instance of the encapsulating class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What data can a static nested class access from the encapsulating class?

A

Static data and final data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Does the encapsulating class have access to the members of the nested class?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What issue does nested class resolve.

A

When two distinct classes are closely coupled, encapsulation can cause unnecessary overhead when accessing data of each other. Nested class resolves this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a local class?

A

A local class is one that is defined within a code block such as a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What other members can local classes access?

A

They can access local members of the method which are declared as final. They can also access instance members of the outer class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an anonymous class? When are they used?

A

Anonymous classes are declared and instantiated at the same time. They are used when the class is only needed once. They can either implement an interface or extend an existing class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly