Abstract Modifier Rules Tutorial

I recommend watching my Introduction to the Abstract Modifier before continuing with this tutorial. In that tutorial I described how an Airplane, Helicopter, and a Glider all have an IS-A relationship with a Flying Machine. I also stated that a Boeing 787, FA18 Fighter, and the Cessna Amphibian all have an IS-A relationship with an Airplane. In the abstract Airplane class I created two abstract methods, takeOff and land. I also stated that the Flying Machine would be a perfect candidate to be a superclass. It makes sense that the abstract Airplane class can be derived from an abstract FlyingMachine class. The FlyingMachine class would become even more abstract than the abstract Airplane class. One thing that I can think of that all flying machines have in common is that they must take off; landing may not apply to all flying machines, take a satellite for example. It flies around the earth at a really high altitude, but it will never land. The engineers who make satellites do not design them to have landing capabilities. So it would not make sense to force all subclasses of Flying Machine to have a land method, whereas all Airplane subclasses should require a land method.
Here are some of the rules that apply to using the abstract modifier:

  • An abstract method cannot have a method body and it must have a semicolon directly after the signature.
  • If at least one method inside of a class is marked abstract, then the class must be marked abstract also.
  • Applying the abstract modifier to a class prevents the class from being instantiated. An abstract class can only be inherited.
  • Abstract methods must be overridden in a concrete subclass. A concrete subclass is simply a class without the abstract modifier that extends an abstract class. If a concrete subclass does not override an inherited abstract method then a compiler error will occur.
  • An abstract class may contain static variables (class variables) and static methods.


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 AbstractRules
C:\Java>cd AbstractRules
C:\Java\AbstractRules>Notepad AbstractRules.java

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


class AbstractRules {
    public static void main(String args[]) {
        Boeing787 commercial = new Boeing787();
        commercial.takeOff();
        commercial.land();

        Satellite hubble = new Satellite();
        hubble.takeOff();

        System.out.println("\nNumber of flying objects in this program: " + FlyingMachine.thingsFlyingAround);
    }
}

abstract class FlyingMachine { // If at least one method inside of a class is marked abstract, then the class must be marked abstract also.
    static int thingsFlyingAround = 0; //An abstract class may contain static variables (class variables) and static methods.
    { thingsFlyingAround++; } // instance initialization block

    abstract void takeOff(); // An abstract method cannot have a method body and must have a semicolon directly after the signature.   
}

abstract class Airplane extends FlyingMachine { // If at least one method inside of a class is marked abstract, then the class must be marked abstract also.
    abstract void land(); // An abstract method cannot have a method body and must have a semicolon directly after the signature. 
}

class Boeing787 extends Airplane { // A concrete subclass is simply a class without the abstract modifier that extends an abstract class.

    //Airplane a = new Airplane(); //NO - Applying the abstract modifier to a class prevents the class from being instantiated.

    @Override
    void takeOff() { // Abstract methods must be overridden in a concrete subclass.
        System.out.println("The 787 needs a very long runway to takeoff ...");
    }
    @Override
    void land() { // Abstract methods must be overridden in a concrete subclass.
        System.out.println("The 787 needs a very long runway to land ...");
    }    
}

class Satellite extends FlyingMachine {
    @Override
    void takeOff() {
        System.out.println("A satellite is propelled into space on a rocket and placed into orbit ...\n some fly high above the earth forever.");
    }
}


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


C:\Java\AbstractRules>javac AbstractRules.java
C:\Java\AbstractRules>java AbstractRules
The 787 needs a very long runway to takeoff ...
The 787 needs a very long runway to land ...
A satellite is propelled into space on a rocket and placed into orbit ...
some fly high above the earth forever.

Number of flying objects in this program: 2


Final thoughts

Abstract methods are an important tool for design functionality; they allow you to envision a broader purpose for your class and set concrete requirements for any subclass that may inherit it.


Tutorials