Lab 04 - Wildcard,Nested Class, Java package
Wildcard
The goal of wildcard in Java is to make generics covariant.
Set notation
Upper bounded wildcard (
? extends T): It represents a set of type , where is the subtype ofT.Lower bounded wildcard (
? super T): It represents a set of type , where is the supertype ofT.
A classic problem is to find the range of a type parameter given a series of wildcards. (This is to determine which type you should declare a variable to to be safe to hold the element you get from a sequence).
The classic solution is to solve a system of inequalities.
Classic Examples
In Exercise 4 - Box, we have a method called Transformer<X, Y> which represents a unary transformation . This question actually requires us to find the most relaxed bounds for and . To do so, let's try the method of solving the inequalities:
should be able to "eat" everything of type
T, soX:>T.The range of should be a subset of
U, soY<:U.Therefore, we should write
Transformer<? super T, ? extends U>.

Methods take in reference type as argument
When you write a method with arguments being a reference type f(Object o), always check whether o is equal to null at first!
Functional Programming
A classic way to describe a function . This means that if our function is deterministic, the previous formula is saying: if you fix a function and supply it with any , can always find the correct .
But we can also say it another way: if you fix a "seed" and supply it with any deterministic function , can always find its image , through .
So, there's nothing stopping us from writign as .
To put it simply, our starting point changes from the function to argument .
Java package
In Java, a package is a way to group closely related classes together. Usually, classes in the same package have high dependency among one another and contain logic/data for performing a specific type of tasks.
For example, java.io is a package containing all classes
related to IO, and java.utilcontains many utility data
structures. (Useful in CS2040S).
default access modifiers
When a class/field/method has no access modifier, it’s
said to have default accessibility: it can be accessed by
any member from the same package.
Classes not belonging to any package are put into the default package by Java. This is why for the previous exercises, not having access modifiers is equivalent to being public (for class/fields/methods).
protected access modifiers
protected access modifiersThe protected means being accessible by any member from the same package and child classes outside the package.
Nested Class
In practice, the use of nested class is called the discriminated union. This is a design of having an abstract parent class with a few sealed inner classes. And its use cases are:
An instance of the parent class is always one of the few subtypes.
Each subtype consists of exactly the same functionalities of the parent class.
The behaviours between different subtypes are mutually exclusive. In other words, the behaviour of the parent class is completely partitioned into disjoint cases.
For example,
Last updated