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>
|
How can you avoid handing out references to mutable objects from a imuatble obect?
- Provide other methods, eg. getNumberOfAnimals(), getAnimal(idx)
- Return a unmodifiable list: Collections.unmodifiableList
- ALSO MAKE SURE TO CREATE A COPY OF THE DATA PASSED VIA CONSTRUCTOR!
- if eg. a List is passed to the costructor and the list is assigned to a final member, the original list can still be changed, making the immutable object mutable
Can abstract classes be used as a functional interface?
No
When can the Diamond operator be used?
When the type can be infered, eg. on the right-hand side when assigning a local variable or field. Also as a parameter on a method call.
What is Type Erasure?
Type Erasure means, that Generic Type information is removed after the compilation and is not available at runntime. This has implications on Wildcard Generics.
Before Compilation: List<A> l = new ArrayList<A>(); A a = l.get(0)
After Compilation: List l = new ArrayList(); A a = (A) l.get(0);
How can you handle a generic type when implementing a generic interface?
- Specify the type, eg: class Foo implements Baa<Concrete> {}
- Create a generic class, eg. class Foo<X> implements Baa<X> {}
- Don't use generics at all: eg. class Foo implements Baa {}
- Results in a compiler warning!
What is a raw type and what happens when you use them?
- A generic type parameter is ommited, eg. new ArrayList()
- Raw Types generate a compiler warnung
What does not work with generics?
- Call a Constructor of a generic type (due to Type Erasure), eg. new T()
- Create an array of the generic type
- Call instanceof, eg boolean b = a instanceof List<String>; (just use List)
- Use a primitive as a generic type parameter
- Creat a static variable as a generic type parametern as the type is linked to the instance
Where is the generic type specified on a nethod declaration?
It is specified immediately before the return type of the method
eg. public static <T> void sink(T t){}
Do you have to declare the generic type when invoking a generic method?
No, but you can:
eg. Box.<String>ship("package");
Does this code compile:
import java.util.*;
public class Main {
static class Dragon { }
static class Unicorn { }
public static void main(String... args) {
List unicorns = new ArrayList();
unicorns.add(new Unicorn());
printDragons(unicorns);
}
private static void printDragons(List<Dragon> dragons) {
for (Dragon dragon : dragons) {
System.out.println(dragon);
}
}
}
Yes it compiles but with a compiler warning: Note: Main.java uses unchecked or unsafe operations.
But the code Fails with a ClassCastException at Runtime. Java doesn't know this promblem until runtime due to type erasyre
Does this code compile?
import java.util.*;
public class Main {
public static void main(String... args) {
List numbers = new ArrayList();
numbers.add(5);
int result = numbers.get(0);
}
}
No, autoboxing does not work here: incompatible types: Object cannot be converted to int
What kind of bounded parameter types exist in Java?
- Unbounded wildcard
- Wildcard with an upper bound
- Wildcard with a lower bound
How do you declare a Wildcard with a lower bound?
eg, List<? super Exception> l = new ArrayList<Object>();
How do you declare a wildcard with an upper bound?
eg, List<? extends Exception> l = new ArrayList<RuntimeException>();
How do you declare a wildcard with no bound?
eg, List<?> l = new ArrayList<String>();
Does this code compile?
import java.util.*;
public class Main {
public static void main(String... args) {
List<String> keywords = new ArrrayList<>();
keywords.add("java");
printList(keywords);
}
private static void printList(List<Object> list) {
for (Object x: list) System.out.println(x);
}
}
No, the compiler enforces an exact match for unbounded wildcards.
The Reason for this is to not have the same behaviour as with Arrays:
Integer[] numbers = {};
Object[] objects = numbers;
objects[0] = ""; // throws ArrayStoreException
Given a class hierarchy of C extends B extends A, what are valid values for X
ArrayList<? extends B> l = new ArrayList<X>();
ArrayList<? extends B> l = new ArrayList<B>();
ArrayList<? extends B> l = new ArrayList<C>();
Given a class hierarchy of C extends B extends A, does this code compile?
public static void main(String... args) {
ArrayList<? extends B> l = new ArrayList<B>();
l.add(new B());
}
No, upper bounded wildcards are read only
Given a class hierarchy of C extends B extends A, does this code compile?
ArrayList<? super B> l = new ArrayList<B>();
B b = l.get(0);
No, lower bounded wildcard values are write only
No, upper bounded wildcards are read only
With List<? super Number>, the acutal list can be of Type List<Number> or List<Object> - meaning that we know what is possible to insert. We do not, however, know what type we receive: A Numer? or an Object?
Does this code compile?
ArrayList<? super B> l = new ArrayList<? super B>();
No, the right hand side cannot be a wild card type
Does this code compile?
<X> void method(List<X super B> list){}
No, the method-specific type parameter is mixed with a wildcard. A wildcard must hava ? in it
Is List a class or an interface
Interface
is Collection a class or an interface?
Interface
Does Map implement the Collection Interface?
No
What's the Signature of the add Method of the Collections-Interface?
boolean add(E element)
What's the Signature of the remove Method of the Collections-Interface?
boolean remove(Object object)
What's the Signature of the clear Method of the Collections-Interface?
voic clear()
What's the Signature of the contains Method of the Collections-Interface?
boolean contains(Object o)
What's the Signature of the add Method of the List-Interface?
void add(int index, E element)
(boolean add(E element) is specified in the Collections Interface which is extended by List)
What's the Signature of the remove Method of the List-Interface?
void remove(int index)
(and the boolean remove(Object o) method inherited from collection)
What's the Signature of the set Method of the List-Interface?
E set(int index, E e)
What does the lower method of the NavigatableSet-Interface do?
Returns greatest element that is < e or null if no such element
What does the floor method of the NavigatableSet-Interface do?
Returns greatest element that is <= e or null if no such element
What does the ceiling method of the NavigatableSet-Interface do?
Returns the least element that is >= e (greater or equal) or null if no such element
What does the higher method of the NavigatableSet-Interface do?
Returns least element that is > (strictly greater) e or null if no such element
What Queue methods throw an exception if the operation fails?
- add
- remove
- element
What Queue methods return a special value if the operation fails?
- offer
- poll
- peak
What does the element method of the Queue-Interface do?
Retreives but does not remove the head of this queue.
What does the offer method of the Queue-Interface do?
Inserts the specified element into this queue (at the end)
Which 2 important methods does the Deque interface add to the Queue interface
- push -> Adds the element at the head of the queue (equivalent to addFirst)
- pop -> Removes the next element (equivalent to removeFirst)