-
-
Save lucamolteni/84e01ad022411270e41a40826587c603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Animal<F extends Food> { abstract void eats(F f);} | |
abstract class Food {} | |
class Veg extends Food {} | |
class Meat extends Food {} | |
class Grass extends Veg {} | |
class Carrot extends Veg {} | |
class Cow extends Animal<Veg> { void eats(Veg f) {}} | |
class Main { | |
public static void main(String[] args) { | |
Cow cow = new Cow(); | |
cow.eats(new Meat()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Money<A extends Money<A>> { abstract A add(A m); } | |
class Fiat extends Money<Fiat> { | |
@Override | |
Fiat add(Fiat m) { | |
return m; | |
} | |
} | |
class Metal extends Money<Metal> { | |
@Override | |
Metal add(Metal m) { | |
return m; | |
} | |
} | |
class Main { | |
public static void main(String[] args) { | |
Fiat f = new Fiat(); | |
Fiat f2 = new Fiat(); | |
Fiat ff = f.add(f2); | |
System.out.println(add(f2, ff)); | |
} | |
static <T extends Money<T>> T add(T a, T b) { | |
return a.add(b); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment