- Statically typed language: variables have definite types, it is a compile-time error to assign a value of an incompatible type to a variable, e.g., Java
- Dynamically typed language: only check type compatiblity at runtime, e.g., JavaScript
- Mutliple Inheritance of State, Implementation, and Type
- Top-level types:
- Primitive types
- Reference types
- Array
- Class
- enums are a special kind of of classes, implicitly extend
java.lang.Enum
- enums are a special kind of of classes, implicitly extend
- Interface
- annotations are a special kind of interfaces, implicitly extend
java.lang.annotation.Annotation
- annotations are a special kind of interfaces, implicitly extend
- Nested types:
- Static member types
- Nonstatic member classes
- Local classes
- Anoymous classes
-
Three types of inheritance are supported for classes: single, multilevel and hierarchical. Let A, B, C being classes
- single
A <-- B
- multilevel
A <-- B <-- C
- hierarchical (tree)
A <-- B, A <-- C
- single
-
Multiple inheritance is NOT supported for classes
- multiple
A <-- C, B <-- C
- why Java doesn’t support multiple inheritance?
- the diamond problem:
A <-- B, A <-- C, B <-- D, C<-- D
- if B and C both have a method with same signature foo(), call D.foo will be a problem wether to call B's foo or C's foo method
- simplicity:
- multiple inheritance creates problem in casting, constructor chaining and so on.
- very few scenarios need multiple inheritance
- the diamond problem:
- multiple
-
Multiple inheritance is supported for Interfaces
- default methods (introduced in Java 8, before interfaces only have abstract methods)
- Type parameter
<T>
always stand in for reference types. It is not possible to use a primitive type as value for a type parameter.- bounded type parameter:
<T extends Number>
- mutiple bounds:
<T extends A & B & C>
- subtyping
- bounded type parameter:
- Type Inference
- Type Erasure
- Wildcards
<?>
- bounded wildcards:
<? extends Number>
- subtyping
- bounded wildcards: