Java Class Design Flashcards

1
Q

What happens when a member of the class has no access modifier?

A

It’s default

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

What is the difference between protected and default access?

A

Protected members are accessible in the subclass, whereas the default members are not

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

Polymorphism can be of two forms: dynamic and static.

What is the difference between them?

A

Dynamic polimorphism: When different forms of a single entity are resolved during runtime

static pol.: When forms of a single entity are resolved at compile time (ex. function overloading, abstract methods)

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

What are the key points for overriding a method?

A
  1. Should have the same argument list types (orcompatible types) as the base version
  2. should have the same return type (can be a subclass - covariant return types)
  3. should not have a more restrictive access modifier than the base version, but may have a less restrictive access modifier
  4. Should not throw new or broader checked exceptions, but may throw fewer or narrower checked exceptions or any unchecked exceptions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the signature of the method equals?

A

public boolean equals(Object obj)

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

What are the key points of making a class immutable?

A
  1. Make the fields final and initialize them in the constructor
  2. make sure the methods dont change the contents inside those mutable objects
  3. dont share the references outside the class, ex as a return value from methods in that class
  4. if you must return a reference, return the deep copy of the object
  5. provide only getters, in case changes must be made to the contents of the object, create a new immutable object with the necessary changes and return that reference
  6. declare the class final
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is encapsulation

A

combining data and the functions operating it as a single unit

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

What are the four flavors of nested classes in java?

A
  1. Static nested class
  2. Inner class
  3. Local inner class
  4. anonymous inner class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Where can you defined a static nested class(or interface)?

A

you can define a class ( or interface) as a static member inside another class (or interface)

the combinations:

class Outer { // an outer class has a static nested class
 static class Inner {}
}
interface Outer { // an outer interface has a static nested class static class Inner {}
}
class Outer { // an outer class has a static nested interface static interface Inner {}
}

interface Outer { // an outer interface has a static nested interface static interface Inner {}
}

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

Where can you define a inner class?

A

You can define a class (or interface) as a non static member inside another class

class Outer { // an outer class has an inner class
 class Inner {}
}
class Outer { // an outer class has an inner interface
 interface Inner {}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to instantiate a static nested class?

A

OuterClassName.NestedClassName staticNested = new OuterClassName.NestedClassName()

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

how to instantiate a inner class?

A

center = this.new Point(x, y);

this.new InnerClass();

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

Which three kinds of methods can interface have?

A

Abstract, default and static methods

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

Whats the difference between abstract classes and interfaces?

A

Fields: abstract class can have static and non-static fields. Interfaces cannot cannot have non-static fields

constants: abstract class can have both static and non static constants. Interfaces can only have static constants
constructor: you can define a constructor in an abstract class. Cannot defined/declare constructor in an interface

acess specifiers: you can have private and protected/public members in an abstract class. cannot have any private or protected members in an interface

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

What are default methods?

A

default methods are instance methods. Inside the default method, this keyword refers to the declaring interface. default methods can call methods from the interface they are enclosed in.

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

What is a functional interface?

A

A functional interface specifies only one abstract method. Sometimes they are known as SAM (Single abstract method) type or interface.

However, it may have any number of default or static methods defined in it

17
Q

How is the @FunctionalInterface used?

A

To affirm that an interface is a functional interface. The compiler will assure that or else it will throw compile error

18
Q

What is the lambda syntax?

A

LambdaParameters -> LambdaBody

LambdaParameters: Are parameters to the lambda function are passed within opening parenthesis. When more than one parameter is passed, they are separeted by commas.

LambdaBody: can be an expression or block. The body could consist of single statement(in this case no explicit curly braces definining a block are required. Such a lambda body is known as “expression lambda”. If there are many statements in a lambda body, they need to be in a block of code.

19
Q
interface LambdaFunction {
 void call();
}

public static void main(String []args) {
LambdaFunction lambdaFunction = () -> System.out.println(“Hello world”);
lambdaFunction.call();
}

How does the lambda expression relate to the function interface LambdaFunction?

A

It is through the single abstract method inside the LambdaFunction interface: void call(). The signature of this abstract method and the lambda expression must match.

  • The lambda expression has () indicating it has no params. It matches with the call method that takes no parameters
  • There is no return statement in this lambda expression body and hence the compiler infers the return type of this expression as void type - that matches with the return type of the call method
20
Q

What is effectively final variables in lambdas?

A

Lambda functions can refer to local variables from the enclosing scope. The variables need to be explicitly declared final or that variable will be treated as effectively final final. Which meeans that the compiler treats the variable as final variable and will issue an error if we try to modify it within the lambda function or in the rest of the function

21
Q

What happens if a lambda expression throws an exception?

A

If it is a checked exception, then the method in the functional interface should declare that; otherwide it will result in a compiler error

22
Q

Are inner classes allowed to contain static methods or static variables?

A

NO

23
Q

Do abstract enum methods require each enum type to implement the method?

A

Yes

24
Q

What is a member inner class?

A

Is a class that is defined at the same level as the instance variables

25
Q

What is a local inner class?

A

Is a class defined within a method

26
Q

What is an anonymous inner class?

A

Special case of a local inner class that does not have a name

27
Q

What are the attributes of a Member inner class?

A
  • Can be declared public, private , protected or default
  • can extend any class and implement any interface
  • can be abstract or final
  • cannot declare static fields or methods
  • can access members of the outer class including private members
  • since a member inner class is not static, it has to be used with an instance of the outer class
  • inner class can have the same variable name as the outer class
Outer outer = new Outer;
Inner inner = outer.new Inner();
28
Q

What are the attributes of a Local inner class?

A
  • Is a nested class defined within a method
  • does not exist until the method is invoked
  • they do not have access specifiers
  • they cannot be declared static and cannot declare static fields or methods
  • they have access to all fields and methods of the enclosing class
  • they do not have access to local variables UNLESS those variables are final or effectively final
  • they can extend any class or any numbers of interfaces
  • can be abstract/final
  • cannot declare static methods
29
Q

What are the attributes of a anonymous inner class?

A
  • cannot have access modifiers
  • cannot declare any number of classes/interfaces, must have exactly one superclass or one interface
  • cannot be abstract/final
  • can access instance members of the enclosing class
  • can access local variables of enclosing class if they are final or effectively final
  • cannot declare static methods
30
Q

What are the attributes of a Static nested class?

A
  • can be public, protected, private or default
  • can extend any number of interfaces
  • can be abstract/final
  • cannot access instance members of the enclosing class, it requires an instance of the enclosing class
  • cannot access local variables of enclosing class
  • CAN declare static methods
31
Q

What are the attributes of an enum?

A
  • enum constructors are always private
  • implicitly final
  • cannot extend an enum
  • can’t clone
  • compiler provides two static methods, values() and valueOf(string) -> throws IllegalArgumentException
  • it implements Comparable, thus can be added to SortedSet, TreeSet and TreeMap
32
Q

Can a static nested class contain a non-static inner class?

A

Yes

33
Q

What are the two exceptions that the wait() method can throw?

A
  • InterruptedException (if it is interrupted by another thread)
  • IllegalMonitorStateException (it is thrown if it is not called in a synchronized block)