Advanced Development in Java Flashcards

1
Q

What is a nested class?

A

A nested class is one where we put the class definition of a class inside the definition of another class.

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

Can a class nested inside another access its private variables?

A

Yes, any class nested within another may access any of its functionalities. The nested class is simply there for the sake of abstraction.

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

Why may you need to use a nested class?

A

You typically want classes to be specialised to a certain task. When you have two classes that rely heavily on each other, it can get annoying having to declare getters/setters or other access functions every time you want to add a new thing.

Nesting the class allows them to share functionality amongst themselves while also ensuring sound encapsulation.

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

What is the difference between a static nested class and an inner class?

A

Static nested classes do not have access to instance members, while inner classes do.

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

What access modifiers may a nested class have?

A

A nested class may have any of the standard modifiers, as it is treated like a variable of the outer class it is within.

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

Can inner classes be accessed from the outside?

A

Yes, as long as they have the appropriate visibility modifier.

One may be instantiated, for example, in the following form: OuterClass.new InnerClass();

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

How does the ‘this’ keyword work with inner classes?

A

‘this’, when cast in the inner class, refers to the inner type instance.

OuterClass.this, then, refers to the instance of the surrounding class.

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

What is a local class?

A

A local class is defined, and exists solely within, a block (typically a method).

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

Can local classes have access modifiers?

A

They may not, as they may not be accessed from anywhere other than the private scope.

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

Can local classes access method-scoped variables?

A

No, unless they are declared as final. The class is its own encapsulated “zone”.

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

What is an anonymous class?

A

An anonymous class is a class declared without any class name at all.

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

How may an anonymous class be instantiated?

A

Anonymous classes are instantiated by creating an instance of an interface, and filling out the functionality required within curly brackets.

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

Where may an anonymous class be instantiated?

A

An anonymous class may be instantiated anywhere with a scope.

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

What is an enumerated class?

A

An enumerated class is a data type consisting of a set of named variables with a specific range.

They are not instantiated, and therefore are effectively static and final.

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

Can enumerated classes be declared locally?

A

No. They must be declared as a class or a class member, because they are static and final.

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

What may an event/listener system be used for in Java?

A

Events and listeners are used to solve a problem where your program will have to wait for some thing to happen.

17
Q

What is event-driven programming?

A

Event-driven programming entails having an action wait for an event to happen. Instead of checking at intervals and wasting memory, we release our resources, and let the program tell it when an event happens.

18
Q

Where may you use an event/listener?

A

A GUI, where you press a button and something happens. In this case, the event of a button being pressed would call an action or method.

19
Q

What is the delegation model in Java?

A

Upon a change in the internal state of an event source, an event is generated, which is an object that typically contains information about said state change. A listener, that has been registered to the event, is then notified of that event happening.

20
Q

How do you register a listener in Java?

A

An event source, such as a button, will have an addCategoryListener() method, where Category may be an Action, a Key, etc.

An object that extends CategoryListener may then register itself (or have itself registered) using that method. When that event is called, it will then activate the categoryPerformed(ActionEvent e) method, where CategoryEvent is our distributed Event object.

21
Q

How may we read the contents of an Event object?

A

When the Event source generates an Event object, it will get passed in as an argument to the categoryPerformed method. We can then read the contents.

22
Q

How could we create our own Event system?

A

We need…

..a Listener class that extends EventListener
..a corresponding Event class that extends Event
..and a Listener instance that extends your created Listener class.

23
Q

What are generics?

A

Generics allow algorithms to be written for types that may be specified at a later point. They let you reuse the same code with different input types.

24
Q

How may we declare a generic type in a class?

A

If the class has an identity of Class<T>, then we may choose for one of our variable types to simply be T variable.</T>

25
Q

How may we declare a generic method?

A

A generic method is declared slightly differently than a generic type, where we place the type before the function name.

e.g. object.<String>functionName("Hello!")</String>

26
Q

What are bounded types?

A

Bounded types are used to allow a class/method to operate not just with generic types, but with types that exhibit certain properties.

We may do this by declaring that <S>, meaning that S must be equal to or a subclass of Class. We may also do this for interfaces.</S>

27
Q

How does the compiler use type inference to help with your generic types?

A

If your method has some input, the compiler will infer the intended generic type based on what the method block contains.

When instantiating a class with a generic type, you may leave the generic bounds (<>) empty.
e.g. Class<int> class = new Class<>()</int>

28
Q

Is Container<Double> a subtype of Container<Number>?</Number></Double>

A

No. This is because the compiler cannot use the given metadata to be sure that the Double is of type Number.

29
Q

Is Container<? extends Number> a subtype of Container<Number>?</Number>

A

Yes. ? is a wildcard, meaning that it is an unknown type to be declared at runtime.

30
Q

What is meant by type erasure?

A

Type erasure is a phenomenon in the Java compiler where when the program is compiled and built, all type information from parameters and arguments within generic classes/methods is erased, allowing the compiler to ensure backwards compatibility with earlier versions.

31
Q

What is known as the raw type?

A

The raw type is the legacy type notation for Java. All generic classes and methods are broken down into raw types at compile time.

32
Q

What is heap pollution?

A

Heap pollution is when a variable of a declared parameterised type refers to an instance that is not of that parameterised type. This is caused when type erasure causes the type information to be unavailable at runtime.

33
Q

What is a collection?

A

A collection is an object that groups multiple elements into a single unit. For example, a collection of cards, or a collection of emails.

34
Q

What is the difference between a collection and a map?

A

A collection is a group of elements, while a map is a grouping of elements that links values known as keys to each element.

35
Q

What is the Set interface?

A

A Set contains no duplicate elements - a disjoint set. Two sets are equal if they contain the same elements, even if the containers are different.

36
Q

What is the List interface?

A

A List is an ordered collection of elements that may contain duplicates. Two sets are equal if they contain the same elements, in the same order.

37
Q

What is the Queue interface?

A

A Queue is a collection of elements, iterated over using first-in-first-out ordering.

38
Q

What is the difference between add/offer, remove/poll and element/peek in a Queue?

A

The former of the two does its task, but returns an Exception upon failure. The latter returns null instead.

39
Q

What is the Deque interface?

A

A Deque is a double-ended queue, where removal can happen at both the head and the tail.