Last active
October 5, 2019 05:37
-
-
Save zllovesuki/f0e3fa7724aa4f6765a2404a2ae19d91 to your computer and use it in GitHub Desktop.
Interface Example
This file contains hidden or 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
public interface MovableObject { | |
void move(); | |
} | |
public abstract class Animal implements MovableObject { | |
abstract public void walk(); | |
} | |
public class Human extends Animal { | |
@Override | |
public void walk() { | |
System.out.println("Human walks using their legs."); | |
} | |
@Override | |
public void move() { | |
walk(); | |
} | |
} | |
public abstract class Car implements MovableObject { | |
abstract public void drive(); | |
} | |
public class Ford extends Car { | |
@Override | |
public void drive() { | |
System.out.println("Ford drives with an engine."); | |
} | |
@Override | |
public void move() { | |
drive(); | |
} | |
} |
This file contains hidden or 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
class World { | |
public static void main(String args[]) { | |
ArrayList<MovableObject> objs = new ArrayList<MovableObject>(); | |
objs.add(new Human()); | |
objs.add(new Ford()); | |
for (MovableObject obj : objs) { | |
obj.move(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment