Skip to content

Instantly share code, notes, and snippets.

@zllovesuki
Last active October 5, 2019 05:37
Show Gist options
  • Save zllovesuki/f0e3fa7724aa4f6765a2404a2ae19d91 to your computer and use it in GitHub Desktop.
Save zllovesuki/f0e3fa7724aa4f6765a2404a2ae19d91 to your computer and use it in GitHub Desktop.
Interface Example
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();
}
}
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