Created
September 18, 2019 14:04
-
-
Save DuncantheeDuncan/b2dafceb0b323f33c499625e9793ca56 to your computer and use it in GitHub Desktop.
this gist gives you a glimpse of how to use Inheritance, polymorphsm and interfaces
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
// this gist gives you a glimpse of how to use Inheritance, polymorphm and interfaces | |
interface Bouncable{ | |
void bounce(); | |
int getBounceFactor(); | |
String getBallName(String name); | |
} | |
class GameWithBouncyBall{ | |
public static void play(Bouncable bouncable){ | |
if (bouncable.getBounceFactor() > 3) { | |
System.out.println("Let's play the game "); | |
bouncable.bounce(); | |
}else { | |
System.out.println("This is not bouncable enough to play this game!"); | |
} | |
} | |
} | |
class BeachBall implements Bouncable{ | |
public String getBallName(String name){ | |
return name; | |
} | |
public void bounce() { | |
System.out.println("Bounce Beach ball"); | |
} | |
public int getBounceFactor() { | |
return 5; | |
} | |
} | |
class CricketsBall implements Bouncable{ | |
public void bounce() { | |
System.out.println("cricket ball bouncing!!"); | |
} | |
public int getBounceFactor() { | |
return 0; | |
} | |
public String getBallName(String name) { | |
return name; | |
} | |
} | |
public class Ball /*implements Bouncable*/{ | |
public static void main(String[] args) { | |
GameWithBouncyBall.play(new BeachBall()); | |
GameWithBouncyBall.play(new CricketsBall()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment