Lamda & Nested Classes Flashcards

1
Q

The purpose of this is to group classes that
belong together, which makes your code more readable and
maintainable.

a class within another class

A

Nested Classes

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

Nested classes are divided into two categories:

A

Static (Static Nested classes).
Non-static (Inner classes).

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

Do not have access to other members of the enclosing
class

class OuterClass {

static class StaticNestedClass {

}
}

A

Static (Static Nested classes).

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

Have access to other members of the enclosing class, even
if they are declared private.

A

Non-static (Inner classes).

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

Why use Nested Classes?

A
  • it is a way of logically grouping classes that are only used in one place.
  • it increases encapsulation
  • it can lead to more readable and maintainable code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If a declaration of a type in a particular scope has the
same name as another declaration in the enclosing scope, then the
declaration shadows the declaration of the enclosing scope. You
cannot refer to a shadowed declaration by its name alone.

A

Shadowing

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

Cannot refer directly to instance
variables or methods defined in its enclosing class: it can use
them only through an object reference:
OuterClass.StaticNestedClass

A static nested class interacts with the instance members of its
outer class (and other classes) just like any other top-level class.
In effect, a static nested class is behaviorally a top-level class
that has been nested in another top-level class for packaging
convenience.
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();

A

Static Nested Classes

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

Because an inner class is associated with an
instance, it cannot define any static members itself.:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

A

Inner Classes

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

two special kinds of inner classes:

A

Local classes
Anonymous classes.

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

classes that are defined in a block, which is a group of zero or more
statements between balanced braces.

A

Local classes

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

enable you to make your code more concise

They enable
you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local
class only once.

A

Anonymous classes

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

Functions as arguments. They provide a more convenient syntax for implementing a functional interface

A

Lamda expressions

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

Any interface that defines exactly one abstract method

A

functional interface

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

Lambda syntax

A

Argument List (a,b)
Arrow toke - >
Body {}

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

Why use lambdas in Java?

A

-A more concise syntax. Lambda expressions allow you to implement a functional interface with fewer lines of code than an
anonymous class.
- Lambdas make it easier to work with collections

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

When a lambda expression simply calls an existing
method you can use a method .. instead..

A

Method References

17
Q

Four different kinds of method references:

A

○ reference to a static method
○ reference to an instance method of a particular object
○ reference to an instance method of an arbitrary object of a
particular type
○ reference to a constructor

18
Q

Enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code.

A

Nested Classes

19
Q

Use this if you need to create more than one instance of a class, access its constructor, or introduce a new, named type (because, for example, you need to invoke additional methods later)

A

Local class

20
Q

Use this if you need to declare fields or additional methods

A

Anonymous class

21
Q

Use this if you are encapsulating a single unit of behavior that you want to pass to other code.

Use this if you need a simple instance of a functional interface and none of the preceding criteria apply.

A

Lambda Expression

22
Q

Use this if your requirements are similar to those of a local class, you want to make the type more widely available, and you don’t require access to local variables or method parameters.

A

Nested class

23
Q

Use this if you require access to an enclosing instance’s non-public fields and methods.

A

non-static nested class (or inner class)

24
Q

Use non-static nested class if you require access to an enclosing instance’s non-public fields and methods.

Use this if you don’t require this access.

A

static nested class

25
Q

The program Problem.java doesn’t compile. What do you need to do to make it compile? Why?

public class Problem {
String s;
static class Inner {
void testMethod() {
s = “Set from Inner”;
}
}
}

A

Delete static in front of the declaration of the Inner class. An static inner class does not have access to the instance fields of the outer class.

public class ProblemSolved {
String s;
class Inner {
void testMethod() {
s = “Set from Inner”;
}
}
}

26
Q

public class Class1 {
protected InnerClass1 ic;

public Class1() {
ic = new InnerClass1();
}

public void displayStrings() {
System.out.println(ic.getString() + ".");
System.out.println(ic.getAnotherString() + ".");
}

static public void main(String[] args) {
    Class1 c1 = new Class1();
c1.displayStrings();
}

protected class InnerClass1 {
public String getString() {
    return "InnerClass1: getString invoked";
}

public String getAnotherString() {
    return "InnerClass1: getAnotherString invoked";
}
} }
A

InnerClass1: getString invoked.
InnerClass1: getAnotherString invoked.

27
Q
A