Last active
August 29, 2015 14:21
-
-
Save Redth/6869f8bc3556cb047828 to your computer and use it in GitHub Desktop.
Covariant Types: C# vs Java
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
public abstract class Animal | |
{ | |
public abstract string Name { get; } | |
} | |
public abstract class AnimalFactory | |
{ | |
public abstract Animal Create (); | |
public abstract void Process (Animal animal); | |
} | |
public class Cat : Animal | |
{ | |
public override string Name { get { return "Cat"; } } | |
} | |
public class CatFactory : AnimalFactory | |
{ | |
// Can't return Cat, must be Animal | |
public override Animal Create () | |
{ | |
return new Cat (); | |
} | |
// Can't pass in Cat, must be Animal | |
public override void Process (Animal animal) | |
{ | |
} | |
} |
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
public abstract class Animal { | |
public abstract String getName (); | |
} | |
public abstract class AnimalFactory { | |
public abstract Animal create (); | |
public abstract void process (Animal animal); | |
} | |
public class Cat extends Animal { | |
@Override | |
public String getName () { | |
return "Cat"; | |
} | |
} | |
public class CatFactory extends AnimalFactory { | |
// We can return Cat here instead of Animal | |
@Override | |
public Cat create () { | |
return new Cat (); | |
} | |
// Still must pass in Animal here | |
@Override | |
public void process (Animal cat) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment