Nested Classes Flashcards

1
Q

Give 3 reasons why we use nested classes

A

Closely linked classes can be located in one place
It can increase encapsulation
Makes code more readable

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

How can enums be declared

A

As a separate class or a class member - not internally within a method

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

1) How are anonymous classes declared
2) Where can they be defined
3) When do we usually use anonymous classes

A

1) Without the keyword “class”; using the keyword “new” followed by the interface that you want to be implemented or the class that you want to extend
2) Within a method or within an argument of a method
3) When only instantiating the class once; they both declared and instantiated in a single statement
(You can’t pass parameters to it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Local classes:
What are they
What modifiers can be used with them
How do you access method local variables
How do you access members of the outer class
A

1) Classes within methods
2) Cannot contain any modifiers
3) If they are declare final in the method that the local class is declared in
4) OuterClass.this.varName

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

Describe scoping (where things can be used in a program) of the key word “this” with inner and outer classes

A
Outer class instances: use OuterClass.this
Inner class instances: use this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create instances of inner classes from inside the outer class and from outside the outer class

A
Inside the outer class:
    Construct instance of inner class directly
Outside the outer class:
    Construct instance of outer class
    Construct instance of inner class using the newly constructed outer class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you create an object for the static nested class in the following code:

class Car {
    ....
    static class Gearbox {
        ....
        }
}
A

Car.Gearbox varName = new car.GearBox();

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

How many files do inner classes produce on compilation

A

2:
OuterClass
OuterClass$InnerClass

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

Name two types of nested class and state the modifiers for outer and nested classes.

A

Static nested class => Doesn’t have access to instance members
Inner class => Has access to instance members

Modifiers for outer: public and default
Modifiers for nested: public, private, protected, default

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

How do nested class help overcome

A

The issue of having two tightly coupled classes that heavily rely on one another

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

How does encapsulation get in the way of classes being specialised

A

If we have two tightly coupled classes

They rely heavily on one another
Takes a lot of work to create functions to access each others members
Occurs frequently in GUI programming

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

Give 3 examples of OOP

A

Classes can be reused
Easy to maintain code
Can hide things that don’t need to be exposed

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