Loading...

Java Abstract Classes

Mark a parent class abstract when it must leave at least one method without a body for children to fill. Write abstract class Machine with abstract void horn(); and a normal method park that prints park. Write class Truck extends Machine and give horn a body that prints honk. In Yard.java, build a Truck, call t.horn(), then call t.park(). Compile with javac Yard.java and run java Yard. The screen should show honk, then park. A second demo adds Car so two children each fill horn differently. Leave polymorphism for the next page — this lesson is the unfinished parent method and the child that finishes it.

Fill the abstract method on the child

The enums page listed fixed names. This page leaves a method unfinished on purpose. Open Yard.java. Keep the public class name Yard. Delete leftover enum Level demos if they bury the abstract story, leftover Signal / implements files, leftover path demos, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Declare the abstract parent above the other types:
abstract class Machine {
abstract void horn();
void park() {
System.out.println("park");
}
}
The abstract method ends with a semicolon and has no { } body. The normal method park keeps a full body so every child can reuse it.
Declare the child with extends Machine. Fill horn:
void horn() {
System.out.println("honk");
}
You must fill every abstract method listed on the parent. On this page that is only horn. Do not write new Machine() in main — an abstract class is not built as an object on its own.
Inside Yard.main:
Truck t = new Truck();
t.horn();
t.park();
The first call runs the body on Truck. The second call runs the shared body on Machine.

Yard Truck fills abstract horn then park

java
abstract class Machine {
    abstract void horn();
    void park() {
        System.out.println("park");
    }
}

class Truck extends Machine {
    void horn() {
        System.out.println("honk");
    }
}

public class Yard {
    public static void main(String[] args) {
        Truck t = new Truck();
        t.horn();
        t.park();
    }
}

From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard. Helper types Machine and Truck stay without public on this beginner shape.
The screen shows honk, then park. Walk the run once. Java starts at Yard.main. new Truck() builds a truck that finished the abstract horn. t.horn() prints honk. t.park() prints park from the parent body that already existed. The unfinished method lived only as a requirement until the child wrote the body.
If javac refuses, check three things. First, the parent class line must say abstract class Machine. Second, the unfinished method must say abstract and end with ;. Third, Truck must actually write horn with a body. If you still see MEDIUM from an enum file, switch to the abstract teaching file and run java Yard again. If you try new Machine(), javac refuses — that is expected on this page.
Say the rule out loud while you watch the screen: abstract parent names the missing method, child fills it, shared methods still run. Do not invent a long polymorphism parade of many parent-typed variables yet. That story waits for the next lesson.

Two children fill horn differently

A second program keeps the same abstract Machine. Truck still prints honk. A new Car prints beep. main builds both, calls each horn, then calls park on the truck.

Yard Truck and Car fill horn then park

java
abstract class Machine {
    abstract void horn();
    void park() {
        System.out.println("park");
    }
}

class Truck extends Machine {
    void horn() {
        System.out.println("honk");
    }
}

class Car extends Machine {
    void horn() {
        System.out.println("beep");
    }
}

public class Yard {
    public static void main(String[] args) {
        Truck t = new Truck();
        Car c = new Car();
        t.horn();
        c.horn();
        t.park();
    }
}

Compile with javac Yard.java, then run java Yard. The screen shows honk, then beep, then park. Each child filled the same abstract name with its own print. The shared park body still comes from Machine. You still never write new Machine().
Read the parent once more: one abstract method, one normal method. Read the children: each writes horn, neither rewrites park on this page. That split is the teaching point. If you omit horn on Car while Car extends Machine, javac refuses because every abstract method must be filled.
If leftover enum demos still sit nearby, keep them as history. For this lesson run the abstract teaching file so the screen shows method fills, not MEDIUM. If javac complains about duplicate public classes, keep only Yard public.

Compared with enums and interfaces

On the enums page, Level listed fixed names. On the interfaces page, implements filled empty interface methods. On this page, abstract class plus extends leaves a class method unfinished until a child writes it. You still compile Yard.java and run java Yard. The new tokens are abstract class, abstract void horn();, and a child that supplies the body while reusing park.
If a leftover enum Level file is still open, you will see constants and maybe a switch. Switch to the abstract file so the top shows abstract class Machine and main builds a Truck. Keep polymorphism demos that only shuffle parent-typed variables off this page. Keep keyboard input off this page.

What this leaves for polymorphism

This page finishes abstract methods on concrete children. The next page shows polymorphism, where one parent-typed variable can hold different child objects and call the matching method body. Stay with abstract parents and filled children here. Do not require a list of Machine variables that point at many children yet.
When you later write polymorphism demos, you will still often start from an abstract or ordinary parent method name. Treat this page as the unfinished-method half of that neighboring story. Finish both Yard demos until honk then park — and then honk, beep, park — feel automatic before you open the polymorphism lesson.

Before polymorphism

You can declare abstract class Machine, fill horn on Truck, and see honk then park. You can add Car with its own horn and see honk, then beep, then park. Compile Yard.java and run java Yard for each kept demo in turn. The child finished the abstract method. The next page is polymorphism, where calls through a parent-typed reference pick the child body.
Keep the single-truck demo for a clear abstract fill plus shared park. Keep the two-child demo for a clear pair of fills. Keep polymorphism parade files off this page. Those three habits are the checkpoint before you move on. When honk appears before park after new Truck(), abstract classes did their job for this lesson and you are ready for the next topic about polymorphism.
One last pass through the folder: one public Yard, one abstract Machine, concrete children that fill horn, no new Machine(), no keyboard wait. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still prints the fills you wrote, the abstract contract is solid and the course can move to polymorphism on the next page.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top