Rec 03
Question (without answers):
Answer:
Problems
01. LSP with Exception Handling
The tricky point lies in understanding the question. What the question wants to ask is that: By LSP, we should be able to substitute A in the method bar with a B . After that, nothing more to mention about this problem.
02. Type Parameter with Two Bounds
In this question, we have encountered an interesting question, that is our type parameters has two bounds! For example, <T extends Number & Comparable<T>>. So, what will happen during the Type Erasure?
The trick is that the type erasure process erases T to be the first bound, which in this case is Number. It then type casts the variable to Comparable so that compareTo can be called.
03. Bridge Method*
Suppose we have the following code,
The code after type erasure will becomre
Line 7 to 9 is called the Bridge Method.
A bridge method is always generated when
a type
extends/implements a parameterized type andtype erasure changes the signature of one or more inherited method.
So, actually, this code is neither overriding nor overloading.
Now, if you use the following code
It will print out B.
Although this may seem intuitive, the actual behavior is more subtle due to generics and type erasure. During compilation, the compiler examines class A (the compile-time type of the target) to determine which methods can be invoked. Since there is only one such method, the erased method descriptor void fun(Object) is stored.
At runtime, Java checks class B (the runtime type of the target) for a method matching this descriptor. It finds the bridge method void fun(Object) in B and invokes it. The bridge method then calls B::fun(String) , which prints "B".
Tips
Bounded type parameters with multiple bounds: If multiple bounds exist (e.g.,
<T extends SomeClass & SomeInterface>),Twill be erased the first bound, and then it is type casted to the second bound. Note that the first bound must be a class! It cannot be aninterface, otherwise, a compile error will be generated!Overrloading and Overriding:
Overloading just needs the method name to be the same, but different method signature!
Start to determine whether it is overloading or overriding after type erasure.
Bridge method: A bridge method is always generated when
a type
extends/implements a parameterized type andtype erasure changes the signature of one or more inherited method.
(Include the code for the bridge method in the cheatsheet!)
Last updated