OCP - 1

Classes, Desing Patterns, Principles, Generics and Collections

Classes, Desing Patterns, Principles, Generics and Collections


Set of flashcards Details

Flashcards 124
Language English
Category Computer Science
Level Other
Created / Updated 31.01.2019 / 08.01.2020
Weblink
https://card2brain.ch/box/20190131_ocp_1
Embed
<iframe src="https://card2brain.ch/box/20190131_ocp_1/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

What is the Result of `null instanceOf X` ?

False, an instanceOf check with null always results in false

When are instanceOf checks executed?

  • For Classes: At Compiletime
  • For Interfaces: At Runtime

What is virtual method invocation?

It basically means that Jva will look at subclasses when finding the right method to call (override)

What's the goal of @override?

It is helpful because the compiler can tell you when you've messd up

What does the equals signature look like?

public boolean equals(Object obj)

What is the equals contract?

  • "Should not change at random"
    • reflexive: x.equals(x) == true
    • symmetric: x.equals(y) == y.equals(x)
    • transitive: x.equals(y) == y.equals(z) == z.equals(x)
    • consistent: x.equals(y) == x.equals(y) (does not change when x and y are not modified)
  • An object an null are not equal
    • x.equals(null) must return false

What is the contract of hashCode?

  • The result must not change -> derive it from members that do not change (eg. an id)
  • The hasCode() can use a subset of equals
    • two equal objects must return the same hashcode
  • the hash code can be a constant, altough it is not efficient

What does the hashCode signature look like?

public int hashCode()

How is an enum declared?

By using the enum keyword instead of the class keyword

Are Enum values comparable with ==

yes, think of them as static final constants

What Methods does an enum provide?

  • Static:
    • MyEnum.values(): Returns an array containing the constants of this enum
    • MyEnum.valueOf("string"): Create an enum from a string
  • Instance:
    • MyEnum.FIRST.name(): Returns the name of this enum constant, exactly as declared in its enum declaration
    • MyEnum.FIRST.ordinal(): ordinal of this enumeration constant

What happens when an illegal string value is providee to the Enum valueOf method?

An IllegalArgumentException is Thrown

Can a class or enum extend an enum?

No

What needs special attention when implementing a switch statement with an enum

The enum type is implied in the case statements and it is not allowed to type it!

switch(myEnum){
  case ONE:  // NOT MyEnum.ONE
  // ...
}

Can a constructor of an enum be public?

No, it wont' compile!

What is the output of this program?

public class Main {

  enum LeEnum {
    ONE, TWO, THREE;
    LeEnum(){
      System.out.println(this.name());
    }
  }

  public static void main(String... args) {
    System.out.println("a");
    LeEnum one = LeEnum.ONE;
    System.out.println("b");
  }
}

the first time that we ask for any of the enum values, Java constructs all of the enum values

a
ONE
TWO
THREE
b

Can an enum declare an abstract method?

Yes, but each value must provide an implementation:

public enum Season {
  Winter {
   pubic void print(){}
  }
 pubic abstract void print(){}
}

What is a Nested Class? What kind of nested classes do exist?

A class that is defined within another class, incluiding:

  • Member inner class
  • local inner class
  • anonymous inner class
  • nested static class

What is an inner class?

A netsted class that is not static

  • Member inner class
  • local inner class
  • anonymous inner class

Where is a member inner class declared?

At the member level.

How is a member inner class instantiated from outside of the outer class?

Outer outer = new outer();

Inner inner = outer.new Inner();

inner.go()

Can you nest multiple inner classes?

Yes

Can a member inner class interface be declared as private?

Yes!
public class Outer {
  private interface Secret { // can be private
   public void shh(); // Must be public
  }
}

What is important when working with local inner and anonynous inner classes?

Only effectively final variables from the outer scopes can be used, usually variables declared as final or variables that do not change (effectively final).

Member variables are effectively final

Is a semicolon required after declaring a local inner class?

No

Is a semicolon required when assigning an anonymous inner class?

Yes:

public void leMethod(){
  Inner i = new Abstract(){
    // ... 
  };
}

How may classes/interfaces can be extended/implemented with an anonymous inner class?

ONLY ONE

How may classes/interfaces can be extended/implemented with a local inner class?

as much as you want...

What types of nested classes are allowed to declare static methods?

Only static netsted classes

What types of nested classes are allowed to access local variables of the encosing class?

Only Local inner classes and Anonymous inner classes, but only if the are final or effectifely final

What does hashCode do?

Provide grouping in some collections (Card-Decks)

Is the @FunctionalInterface Annotation required to use lambdas?

No, it is analogous to the @Override interface intended as a marker / check

When can you ommit the parentheses () in a lambda expression

If ther is exactly one parameter and the data type is not specified

Whe can the braces {} be ommited in a lambda expression?

For a single-line lambda body without a return statement (added implicitly)

Can you re-declare a local variable inside a lambdas body?

No, as with any method, you are not allowed to re-declare a variable

Is this lambda declaration valid?

(String a, b) -> a.startsWith(b)

No, you must either declare all types or none

Why should data be encapsulated?

With encapsulation, a class is able to maintain certain invariants about its internal data.

When is is a valid suffix in the JavaBean naming conventions?

Only for methods returning a primitive boolean property (NOT for the Boolean wrapper class)

What has to be considered when implementing layz loading for a singleton

Thread-Safety

How can you create an imutable class?

  • Use a constructor to set all properties of the object
  • Mark all of the instance variables private and final
  • No setters
  • Don't share references to a mutable Object (eg. getList that returns a reference to an ArrayList)
  • make the class final to prevent methods from being overridden