Inner Classes Flashcards

(135 cards)

1
Q

What is a nested class in Java?

A

A nested class is a class declared inside another class.

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

Why are nested classes used?

A

To logically group classes that are only used in one place and improve encapsulation.

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

How many types of nested classes exist in Java?

A

Four: static nested class, inner class, local class, and anonymous class.

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

What is a static nested class?

A

A nested class declared static that does not require an instance of the outer class.

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

What is an inner class?

A

A non-static nested class that requires an instance of the outer class.

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

What is a local class?

A

A class declared inside a method or block.

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

What is an anonymous class?

A

A nameless class declared and instantiated in one expression.

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

Can nested classes access outer class members?

A

Yes. They can access members of the enclosing class, including private ones.

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

Can outer class access nested class members?

A

Yes, depending on access modifiers.

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

Why are nested classes useful for encapsulation?

A

They hide helper classes from other classes.

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

Can nested classes be private?

A

Yes. Nested classes can use any access modifier.

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

Do nested classes improve code readability?

A

Yes. They keep related logic grouped together.

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

Can a file contain multiple classes?

A

Yes, but only one public top-level class per file.

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

Is a nested class the same as a package-level class?

A

No. Nested classes are defined inside another class.

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

When should you use nested classes?

A

When a class is tightly coupled to another and not needed elsewhere.

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

Key rule to remember?

A

Nested class = class inside another class.

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

How many types of nested classes are there in Java?

A

Four types: member inner class, local inner class, anonymous inner class, and static nested class.

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

What is a member inner class?

A

A non-static class defined inside another class and associated with an instance of the outer class.

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

What is a local inner class?

A

A class declared inside a method or block.

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

What is an anonymous inner class?

A

A nameless class declared and instantiated in a single expression.

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

What is a static nested class?

A

A nested class declared static that does not require an instance of the outer class.

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

Which nested class type requires an outer class object?

A

Member inner class.

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

Which nested class can access outer class instance members directly?

A

Member inner class.

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

Which nested class can only access final or effectively final variables from enclosing method?

A

Local inner class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Which nested class is commonly used for event handling or callbacks?
Anonymous inner class.
26
Which nested class behaves like a normal class but is scoped inside another class?
Static nested class.
27
Can static nested classes access instance members of outer class?
No. Only static members directly.
28
Why use nested classes?
To logically group related classes and improve encapsulation.
29
Which nested class type is most flexible?
Static nested class, because it behaves like a regular class.
30
Which nested class type has no name?
Anonymous inner class.
31
Key rule to remember?
Nested classes = Member + Local + Anonymous + Static.
32
Why do we use nested classes in Java?
To logically group related classes, improve encapsulation, and increase code clarity.
33
What is the main design benefit of nested classes?
They keep helper classes closely tied to the outer class that uses them.
34
How do nested classes support logical grouping?
They place classes that work together in one location.
35
How do nested classes improve encapsulation?
A nested class can access private members of its outer class.
36
Why is encapsulation stronger with nested classes?
Because helper classes can be hidden from outside access.
37
How do nested classes improve code organization?
They reduce clutter by keeping related code together.
38
What visibility advantage do nested classes provide?
They can be declared private, protected, or package-private.
39
When should you use nested classes?
When a class is only useful to one enclosing class.
40
Why not always use top-level classes?
Because it exposes implementation details unnecessarily.
41
Do nested classes reduce namespace pollution?
Yes. They prevent helper classes from being visible globally.
42
How do nested classes affect readability?
They make relationships between classes clearer.
43
Can nested classes access outer class private fields?
Yes. They have full access to outer class members.
44
Why are nested classes considered good design practice?
They promote modular, maintainable, and structured code.
45
Are nested classes required for functionality?
No. They are mainly for design and organization benefits.
46
Key rule to remember?
Nested classes improve structure, encapsulation, and readability.
47
What is the difference between a nested class and an inner class in Java?
A nested class is any class defined inside another class, while an inner class specifically refers to a non-static nested class.
48
What is a nested class?
A class declared inside another class, which can be static or non-static.
49
What is an inner class?
A non-static nested class associated with an instance of the outer class.
50
Can all nested classes access outer class members?
Not all. Only inner classes can directly access instance members of the outer class.
51
Can static nested classes access outer instance variables?
No. They can only access static members of the outer class directly.
52
Do inner classes require an outer class instance?
Yes. They are tied to an instance of the enclosing class.
53
Do static nested classes require an outer object?
No. They can be created independently.
54
Which type is more memory efficient?
Static nested classes, because they do not store outer instance reference.
55
Which nested class type can access private members of outer class?
Inner classes can access all members, including private ones.
56
Is every inner class a nested class?
Yes. Inner classes are a subset of nested classes.
57
Is every nested class an inner class?
No. Static nested classes are nested but not inner.
58
Why use static nested classes?
When behavior belongs logically to outer class but doesn’t need its instance.
59
Why use inner classes?
When class must interact closely with outer object state.
60
Key rule to remember?
Nested = general category; Inner = non-static nested class.
61
What is a nested interface in Java?
A nested interface is an interface declared inside a class or another interface.
62
Where can a nested interface be declared?
Inside a class or inside another interface.
63
Is a nested interface static by default?
Yes. Nested interfaces are implicitly static.
64
What does it mean that a nested interface is static?
It does not require an instance of the enclosing class or interface.
65
Can a nested interface be declared private?
Yes, if it is inside a class.
66
Can a nested interface be declared public?
Yes. It can use any access modifier allowed in its context.
67
Can a nested interface access instance members of outer class?
No. Because it is static by default.
68
Can classes implement a nested interface?
Yes. Any class can implement it if it has access to it.
69
How do you reference a nested interface?
Using OuterClass.InterfaceName.
70
Example reference syntax?
Outer.InnerInterface obj;
71
Why use a nested interface?
To logically group related interfaces with their enclosing class.
72
Does a nested interface improve encapsulation?
Yes. It can hide implementation contracts inside a class.
73
Is nested interface the same as inner interface?
No. Java treats nested interfaces as static by default.
74
Can an interface contain another interface?
Yes. Interfaces can contain nested interfaces.
75
Key rule to remember?
Nested interface = interface inside class or interface, implicitly static.
76
How can a local inner class access a local variable in Java?
The local variable must be final or effectively final.
77
What does effectively final mean?
A variable whose value is assigned once and never changed.
78
Why must local variables be final or effectively final?
Because local inner classes capture variable values, not variables themselves.
79
Can a local inner class access non-final local variables?
No. It cannot access variables whose values change.
80
How can you allow access to a local variable inside a local inner class?
Declare it as final or ensure it is effectively final.
81
Does Java still require explicit final keyword?
No. Since Java 8, effectively final variables work without the final keyword.
82
What happens if you modify a captured variable?
The compiler throws an error.
83
Why does Java enforce this rule?
To avoid inconsistent variable states and ensure thread safety.
84
Do instance variables require final to be accessed?
No. Instance and static variables can always be accessed.
85
Do static variables require final to be accessed?
No. Static variables are accessible normally.
86
Does this rule apply to anonymous classes too?
Yes. Anonymous classes follow the same rule.
87
Does this rule apply to lambda expressions?
Yes. Lambdas also require captured variables to be effectively final.
88
What is the main reason for this restriction?
Local variables live on the stack, but inner classes may outlive the method scope.
89
Key rule to remember?
Local inner classes can access only final or effectively final local variables.
90
Can an interface be defined inside a class in Java?
Yes. A class can contain a nested interface.
91
What is an interface inside a class called?
A nested interface.
92
Is a nested interface static by default?
Yes. Interfaces declared inside classes are implicitly static.
93
Can the outer class access the nested interface?
Yes. The enclosing class can always access it.
94
Can other classes access a nested interface?
Yes, if it is declared public or protected.
95
Can a nested interface be private?
Yes. If declared private, only the enclosing class can use it.
96
Do you need an instance of outer class to use nested interface?
No. Because it is static by default.
97
How do you reference a nested interface outside the class?
OuterClass.InterfaceName.
98
Can a class implement a nested interface?
Yes. Any accessible nested interface can be implemented.
99
Why define an interface inside a class?
To logically group it with related functionality and improve encapsulation.
100
Does a nested interface have access to outer instance members?
No. Because it is static.
101
Can nested interfaces have methods?
Yes. They can declare abstract, default, and static methods.
102
Is a nested interface different from an inner class?
Yes. Nested interfaces are always static; inner classes are not.
103
What design benefit do nested interfaces provide?
Better organization and controlled visibility of contracts.
104
Key rule to remember?
A nested interface can exist inside a class and is implicitly static.
105
Do we have to explicitly mark a nested interface as public static?
No. A nested interface is implicitly static, and in interfaces it is also implicitly public.
106
Is a nested interface always static?
Yes. Interfaces declared inside classes or interfaces are implicitly static.
107
Is a nested interface always public?
Inside an interface, yes. Inside a class, it depends on the access modifier used.
108
Are the keywords public and static required for nested interfaces?
No. They are redundant because Java adds them automatically.
109
Can you still explicitly write public static?
Yes. It is legal but unnecessary.
110
What happens if you omit static for a nested interface?
It is still treated as static by default.
111
Can a nested interface be private?
Yes, if declared inside a class.
112
Does a nested interface require an instance of the outer class?
No. Because it is implicitly static.
113
How do you reference a nested interface?
OuterClass.InterfaceName.
114
Can nested interfaces access outer class instance members?
No. They are static by nature.
115
Does implicit static apply to nested classes too?
No. Only nested interfaces are implicitly static.
116
Why did Java designers make nested interfaces static by default?
To avoid coupling them to outer class instances.
117
Is writing public static for nested interface considered bad practice?
Not bad, just redundant.
118
Does this behavior apply to top-level interfaces?
No. Top-level interfaces are implicitly public (if declared public) but not static.
119
Key rule to remember?
Nested interfaces are implicitly static (and public in interfaces), so modifiers are optional.
120
Why do we use Static Nested interface in Java?
Only the enclosing class can access a Static Nested interface. Consider following code in which interface Xyz is enclosed in class Abc. public class Abc { public interface Xyz { void callback(); } public static void registerCallback(Xyz xyz) {...} } // Client Code Abc.registerCallback(new Abc.Xyz() { public void callback() {...} }); Any code that cannot access Abc can not access interface Xyz also. So the purpose of declaring an Inner interface is to restrict its access from outside world.
121
Why do we use a static nested interface in Java?
To restrict visibility and logically group an interface with the class that uses it.
122
What is the main design purpose of a static nested interface?
Encapsulation of contracts inside the enclosing class.
123
How does a nested interface restrict access?
Only code that can access the outer class can reference the nested interface.
124
Why not declare the interface as a top-level interface?
A top-level interface would be globally visible and less controlled.
125
How does a nested interface improve code organization?
It keeps related interfaces close to the class that depends on them.
126
Who can implement a nested interface?
Any class that has access to it based on its visibility modifier.
127
Can a nested interface be private?
Yes. If declared private, only the enclosing class can use it.
128
Why is this useful for API design?
It hides implementation contracts from external consumers.
129
Does a nested interface require an instance of outer class?
No. It is implicitly static.
130
How does it help modular design?
It limits scope of interfaces to only where they are relevant.
131
When is nested interface preferable?
When interface is tightly coupled to one class.
132
Can nested interfaces be used for callbacks?
Yes. They are commonly used for event or callback contracts.
133
Does nested interface improve security?
Yes. It prevents unintended external usage.
134
Is nested interface mainly a structural or functional feature?
Primarily structural for organization and access control.
135
Key rule to remember?
Static nested interfaces are used to encapsulate contracts and restrict visibility.