Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created January 19, 2014 04:41
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 rajeevprasanna/8500554 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/8500554 to your computer and use it in GitHub Desktop.
polymorphism example
package polymorphism;
public interface Animatable {
void animate();
}
package polymorphism;
public class GameShape {
public void displayShape() {
System.out.println("displaying shape");
}
// more code
}
package polymorphism;
public class PlayerPiece extends GameShape implements Animatable {
public void movePiece() {
System.out.println("moving game piece");
}
public void animate() {
System.out.println("animating...");
}
// more code
}
package polymorphism;
public class Test {
public static void main(String[] args) {
// following are legal
PlayerPiece player = new PlayerPiece();
Object o = player;
GameShape shape = player;
// shape.movePiece-- this call will not work as methods on GameShape
// only visible.
// compiler is not aware of what actually object instance type is. at
// runtime, jvm can identify object and then typecast works
Animatable mover = player;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment