An abstract Shape with two concrete shapes
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.
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).
Write an abstract Game with a final play() that calls start(), then takeTurn() three times, then end(). Implement Chess with its own messages.
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
What's an abstract class and why can't you instantiate it?
Can an abstract class have a constructor? When does it run?
Explain the Template Method pattern and why the template method is usually final.
Which modifier combinations with abstract are illegal, and why?
Expected output
Shape constructor for circle
Shape constructor for rectangle
circle: area=3.14 perimeter=6.28
rectangle: area=6.00 perimeter=10.00