JetBrains Academy: Type Erasure - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Type Erasure
Violator:
You are asked to perform security audit in a company that bakes Bakery and different subclasses of it and sells it in a nice boxes. Before release to customer all boxes are checked with carefully designed NaiveQualityControl class. Numerous cases when something other than Bakery (e.g. Paper) was packed in Boxes and escaped the quality check happened recently.
Short look at NaiveQualityControl leads to conclusion that it's quite easy to provide NaiveQualityControl with Box filled with Paper that will pass QC, and you task is to demonstrate this. Code of related classes follows:
/* This class and its subclasses should pass quality check */
class Bakery {}
class Cake extends Bakery {}
/* But this should not */
class Paper {}
/* These boxes are used to pack stuff */
class Box<T> {
void put(T item) { /* implementation omitted */ }
T get() { /* implementation omitted */ }
}
/* This quality checker ensures that boxes for sale contain Bakery and anything else */
class NaiveQualityControl {
public static boolean check(List<Box<? extends Bakery>> boxes) {
/* Method signature guarantees that all illegal
calls will produce compile-time error... or not? */
return true;
}
}
You need to add implementation to Violator.defraud() method that will do the following:
- Create List of Boxes<? extends Bakery> according to method signature
- Put Paper object in at least one Box in the list
- The resulting list should pass NaiveQualityControl check
You shouldn't change method signature or change code of any other classes, just add implementation to defraud method.
/**
* Class to work with
*/
class Violator {
public static List<Box<? extends Bakery>> defraud() {
List defraudBoxes = new ArrayList();
Box box = new Box();
box.put(new Paper());
defraudBoxes.add(box);
return defraudBoxes;
}
}