Skip to content

Instantly share code, notes, and snippets.

@DuncantheeDuncan
Created September 18, 2019 14:04
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 DuncantheeDuncan/b2dafceb0b323f33c499625e9793ca56 to your computer and use it in GitHub Desktop.
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 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