Abstract Classes and Interfaces
Abstract classes share code and force children to implement methods. Interfaces describe abilities and a class can implement many.
Example
public class Main {
interface Flyer { String fly(); }
static abstract class Bird { abstract String name(); }
static class Duck extends Bird implements Flyer {
String name() { return "Duck"; }
public String fly() { return "flies"; }
}
public static void main(String[] args) {
Duck d = new Duck();
System.out.println(d.name() + " " + d.fly());
}
}Result
Duck flies
Exercise
Interface keyword.
class A I {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.