Interface: Abstract Methods Tutorial - Java 8+

Prior to Java 8, the only method allowed in an interface was technically an abstract method. The pre-Java 8 interface methods were implicitly public and abstract ‐ those modifiers are optional and it was common to not explicitly apply them. They still are implicitly public and abstract in Java 8, but because there are now three types of legal methods in an interface I feel that I should refer to them as abstract methods. If it looks like a duck and quacks like a duck, well you know ... anyway, here are the rules that apply to an abstract method in an interface:

  • An abstract method cannot have a method body and it must have a semicolon directly after the signature.
  • An abstract method is implicitly public and abstract. You may explicitly apply both or either one if you like.
  • Abstract methods must be overridden in a concrete subclass. A concrete subclass is simply a class without the abstract modifier that implements an interface. If a concrete subclass does not override an inherited abstract method then a compiler error will occur.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md InterfaceAbstract
C:\Java>cd InterfaceAbstract
C:\Java\InterfaceAbstract>Notepad InterfaceAbstract.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class InterfaceAbstract {
    public static void main(String args[]) {
        new Bobby().sellHouse();
    }
}

class Bobby implements GrandmasWill {
    @Override
    boolean takeCareOfHoardingIssue() {
        System.out.println("Call family and sort through stuff ... no small task!");
        return true; // never ending task
    }
    @Override
    void sellHouse() {
        int i = 1;
        while(takeCareOfHoardingIssue()) {
            System.out.println("Month "+i+" of sorting ...");
            i++;
            if (i==5) break;
        }
        System.out.println("Time to call a realtor.");
        spendInheritance();
    }
    @Override
    public void spendInheritance() {
        System.out.println("I\'m going on a vacation!");
    }
}

interface GrandmasWill {
    boolean takeCareOfHoardingIssue(); // decades of collections
    void sellHouse();
    public abstract void spendInheritance();
}


Now switch back to the command prompt (CMD) and type in javac InterfaceAbstract.java and press Enter.
Now type in java InterfaceAbstract and press Enter.


C:\Java\InterfaceAbstract>javac InterfaceAbstract.java
C:\Java\InterfaceAbstract>java InterfaceAbstract
Call family and sort through more stuff ...
Month 1 of sorting ...
Call family and sort through more stuff ...
Month 2 of sorting ...
Call family and sort through more stuff ...
Month 3 of sorting ...
Call family and sort through more stuff ...
Month 4 of sorting ...
Time to call a realtor.
I'm going on a vacation!


Final thoughts

Simple recap - don't forget about the implicit public and abstract.


Tutorials