Introduction to the Abstract Modifier

The topic of the abstract modifier is especially confusing when learning object-oriented programming. There are many rules to memorize for the proper use of the abstract modifier. Instead of just making a giant list of the rules, I am going to spread abstract methods and classes over several tutorials.
I am going to quickly introduce you to the concept of IS-A and HAS-A; they literally mean "is a" and "has a" and they will simplify your life. Let's play a little game where you compare the relationship of two objects by placing "is a" and "has a" between them and you pick the combination that sounds right.

Airplane         is a     Flying Machine       YES
Airplane         has a    Flying Machine       NO
Helicopter       is a     Flying Machine       YES
Helicopter       has a    Flying Machine       NO
Glider           is a     Flying Machine       YES
Glider           has a    Flying Machine       NO
Boeing 787       is a     Airplane             YES
Boeing 787       has a    Airplane             NO
FA18 Fighter     is a     Airplane             YES
FA18 Fighter     has a    Airplane             NO
Cessna Amphibian is a     Airplane             YES
Cessna Amphibian has a    Airplane             NO
Boeing 787       is a     Engine               NO
Boeing 787       has a    Engine               YES
FA18 Fighter     is a     Engine               NO
FA18 Fighter     has a    Engine               YES
Cessna Amphibian is a     Engine               NO
Cessna Amphibian has a    Engine               YES
Glider           is a     Engine               NO
Glider           has a    Engine               NO

All right, you get the idea. The Airplane, Helicopter, and Glider all have an IS-A relationship with a Flying Machine. That makes the Flying Machine an ideal candidate to be a superclass.

The Boeing 787, FA18 Fighter, and the Cessna Amphibian all have a IS-A relationship with an Airplane. That makes the Airplane an ideal candidate to be a superclass. That also makes the Boeing 787, FA18 Fighter, and the Cessna Amphibian ideal candidates to be subclasses. Let's think for a moment and make small list of a few things that all airplanes have in common:

  • They all have a model name.
  • They all have a top speed.
  • They all take off.
  • They all land.
Based on our list, we need to ask ourselves a simple question for each list item – "Is this the same for all types of [Airplanes]?" If the answer is yes then we can code the necessary members into our Airplane class. If the answer is yes, but wait. then we need to make an abstract method ... I'll explain.
They all have a model name. Yes., Create a private String model and put in some constructor and accessor code.
They all have a top speed. Yes., Create a private int topSpeed and put in some constructor and accessor code.
They all take off. Yes, but wait. Airplanes can take off in many different ways, we need an abstract method.
They all land. Yes, but wait. Airplanes can land in many different ways, we need an abstract method.

I am now ready walk you through some code and I will touch on a few rules, but keep in mind that I am saving all the rules for other tutorials.



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

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


class AbstractIntro {
    public static void main(String args[]) {
        Boeing787 commercial = new Boeing787("787", 590);
        commercial.summary(); 
        commercial.takeOff();
        commercial.land();

        System.out.println();

        FA18 fighter = new FA18("FA18",1190);
        fighter.summary();  
        fighter.takeOff();
        fighter.land();

        System.out.println();

        CessnaAmphibian caravan = new CessnaAmphibian("Cessna Amphibian", 213);
        caravan.summary();  
        caravan.takeOff();
        caravan.land();

    }
}

abstract class Airplane {
    private String model = "";
    private int topSpeed = 0;

    Airplane() { super(); }
    Airplane(String model, int topSpeed) { 
        this.model = model;
        this.topSpeed = topSpeed;
    }

    void setTopSpeed(int topSpeed) { this.topSpeed = topSpeed; }
    int getTopSpeed() { return topSpeed; }

    void setModel(String model) { this.model = model; }
    String getModel() { return model; }

    void summary() { System.out.println("The top speed of a " + model + " is: " + topSpeed); }

    abstract void takeOff(); // abstract method - no code block/body
    abstract void land();  // abstract method - no code block/body    
}

class Boeing787 extends Airplane {
    Boeing787() { super(); }
    Boeing787(String model, int topSpeed) {
        super(model, topSpeed);
    }

    @Override
    void takeOff() {
        System.out.println("The 787 needs a very long runway to takeoff ...");
    }
    @Override
    void land() {
        System.out.println("The 787 needs a very long runway to land ...");
    }
} 

class FA18 extends Airplane {
    FA18 () { super(); }
    FA18 (String model, int topSpeed) {
        super(model, topSpeed);
    }

    @Override
    void takeOff() {
        System.out.println("The FA18 is launched off the deck of a aircraft carrier like a slingshot ...");
    }
    @Override
    void land() {    
        System.out.println("The FA18 is \'hooked\' by a catch cable when it lands on an aircraft carrier ...");
    }
} 

class CessnaAmphibian extends Airplane {
    CessnaAmphibian () { super(); }
    CessnaAmphibian (String model, int topSpeed) {
        super(model, topSpeed);
    }

    @Override
    void takeOff() {
        System.out.println("The Cessna Amphibian can take off from the water or a runway ...");
    }
    @Override
    void land() {    
        System.out.println("The Cessna Amphibian can land in the water or on a runway ...");
    }
} 


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


C:\Java\AbstractIntro>javac AbstractIntro.java
C:\Java\AbstractIntro>java AbstractIntro
The top speed of a 787 is: 590
The 787 needs a very long runway to takeoff ...
The 787 needs a very long runway to land ...

The top speed of a FA18 is: 1190
The FA18 is launched off the deck of a aircraft carrier like a slingshot ...
The FA18 is 'hooked' by a catch cable when it lands on an aircraft carrier ...

The top speed of a Cessna Amphibian is: 213
The Cessna Amphibian can take off from the water or a runway ...
The Cessna Amphibian can land in the water or on a runway ...


Final thoughts

When a subject is confusing, the best approach to achieve understanding is through repetition. Watch this tutorial again, search on the internet for other resources, eventually it will just click and you will get it. Stay tuned for my next tutorial where I will discuss the rules of the abstract modifier.


Tutorials