Created
January 19, 2014 04:41
-
-
Save rajeevprasanna/8500554 to your computer and use it in GitHub Desktop.
polymorphism example
This file contains 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
package polymorphism; | |
public interface Animatable { | |
void animate(); | |
} |
This file contains 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
package polymorphism; | |
public class GameShape { | |
public void displayShape() { | |
System.out.println("displaying shape"); | |
} | |
// more code | |
} |
This file contains 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
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 | |
} |
This file contains 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
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