Command Palette

Search for a command to run...

Back to the lesson: Topic 5.6 — Abstract Classes
Core Java · Example 3 of 3

Abstract in the middle of a hierarchy

An abstract class is a partly finished class: it can hold fields, constructors and working methods, plus abstract methods with no body that every concrete subclass must fill in. You can't create an object of an abstract class directly.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Create an abstract Employee with name, an abstract double monthlyPay(), and a concrete payslip() returning "<name>: <pay>". Add SalariedEmployee (annual / 12) and HourlyEmployee (rate × hours).

02

Write an abstract Game with a final play() that calls start(), then takeTurn() three times, then end(). Implement Chess with its own messages.

03

Make an abstract Animal with an abstract sound() and a concrete speak() that prints "<class name> says <sound>". Create Dog and Cow and call speak() on both through an Animal[].

Explain it without notes

01

What's an abstract class and why can't you instantiate it?

02

Can an abstract class have a constructor? When does it run?

03

Explain the Template Method pattern and why the template method is usually final.

04

Which modifier combinations with abstract are illegal, and why?

Abstract in the middle of a hierarchy
Sign in to run this example in your browser.

Expected output

PetrolCar: 4 wheels, runs on petrol
ElectricCar: 4 wheels, runs on electricity
Car abstract? true
PetrolCar abstract? false