Skip to content

Instantly share code, notes, and snippets.

@farhadmpr
Created February 2, 2020 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farhadmpr/9b0da3b20f80480ada0d99c12a23769d to your computer and use it in GitHub Desktop.
Save farhadmpr/9b0da3b20f80480ada0d99c12a23769d to your computer and use it in GitHub Desktop.
Replace Conditional Statements (IF/ELSE or SWITCH) With Polymorphism
Interface GameObject() {
increaseSpeed() {};
}
class Rabbit implements GameObject {
increaseSpeed() {
//increase speed by 3
}
}
class Bear implements GameObject {
increaseSpeed() {
//increase speed by 2
}
}
class Turtle implements GameObject {
increaseSpeed() {
//increase speed by 1
}
}
class Handler {
public void increaseSpeed(GameObject go1) {
go1.increaseSpeed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment