Skip to content

Latest commit

 

History

History
55 lines (35 loc) · 1.5 KB

learning_java_generics.md

File metadata and controls

55 lines (35 loc) · 1.5 KB
path title
/learnings/java_generics
Learnings: Java: Generics

Table Of Contents

Generic Declarations <Learning_Java_Generic_Declarations>>

Only an instance method, not on a class

class Thing {
    public <T> void methodName( String thing, T whatsit ) {

    }
}

T must inherit from some base class

public <T extends String> void fooBar(T) { ... }

T must implement some interface

public <T extends String & Runnable> void foobar(T) { ... }

The & signals that the class must implement that thing.

If you want to just check for interface implementation can do extends Object & Runnable.

And Type Erasure <<Java_Type_Erasure>>

See also:

  • <<Kotlin_Type_Erasure>>

Book Recommendations