TYPES, VALUES AND VARIABLES(6)
TYPES, VALUES AND VARIABLES(6)
TYPE VARIABLES:
(Note before: this sectiont contains components that will be expanded on in later blogs. If you have questions about any statement particularly the fact that I decided to produce a skeletal illustration, feel free. )
A type variable is an unqualified identifier. They are introduced by generic class declarations, generic interface declarations, generic method declarations and generic constructor declarations[1].
Let’s think of typical declarations of type variables assuming our type variable is T and T may have superclass S and there are interfaces I1, I2.
a. where T is declaring a class.
class T extends S implements I1, I2 { }
Notes
1. a class can extend only one class, that is a class can have only one superclass otherwise compile time error.
2. extending and implementing are optional contracts T can chose to embark upon, if there is none, then superclass is Object.
3. the order of the bounds S, I1, I2 are important. A class implements only interfaces never another class.
4. if I1, I2 are parameterized types, they should not be the same erasures[2].
b. where T is declaring an interface.
Interface T extends I1, I2 { }
1. (2) above applies here.
2. (4) above also applies here.
3. an interface can extend any number of interfaces.
On the jls: the discussion on §4.4 is quite illustrative but for beginners, might be somewhat confusing, keep this for reference after parameterized types and modifiers are discussed.
Footers:
[1]. Genericity: I can best try to define genericity in this terms, as the ability of a type to define what types it can accept as parameters within its “type entity” (what I can think of here is xml’s namespace).
For example, if a house is built with the stipulation that: “N has the first flat and D has the second flat and that is how it should be”, then we can call this house generic and declare it as,
class house
Generics will be dwelt with in the next blog and later, we’ll discuss the scope of generics.
[2]. Erasures. Erasures are mappings from a generic type to a non-generic one. Assume a parameterized type P

No comments:
Post a Comment