Interfaces, enum. Practical Tasks
Give the SUMMER constant its own body to override getDescription(), and Season.SUMMER.getClass() suddenly returns not Season but an anonymous class Season$1. Details like this are exactly where interfaces and enums catch people out — and the fastest way to get a feel for them is to write the code yourself.
System.out.println(Season.SUMMER.getClass()); // class Season$1
System.out.println(Season.SUMMER.getDeclaringClass()); // class Season This page contains 4 Java interface and enum exercises covering interfaces, the instanceof operator and reference type casting, enums (enum) with fields, constructors and overridden methods, abstract classes and inheritance. The tasks go from easy to hard: from a single Printable interface to a small “online store” application. Every task has a solution on Patreon, and the first two also have a video walkthrough.
Topics You Will Need
Before you start, review the basics of object-oriented programming in Java, abstract classes and reference type casting (upcasting and downcasting).
| Task | Topics | What it practices |
|---|---|---|
| 1. Printable | Interface, array of an interface type, instanceof, static methods | Polymorphism: one print() call — different behavior |
| 2. Seasons | enum, enum field and constructor, switch, overriding a method in a constant | An enum as a full-fledged class |
| 3. Clothing | enum, abstract class, inheritance, implementing multiple interfaces | Designing a class hierarchy |
| 4. Online store | All of the above + keyboard input, enum for menu operations | Building a small application end to end |
Task 1. The Printable Interface
- Define an interface
Printablewith a methodvoid print(). - Define a class
Bookthat implements thePrintableinterface. - Define a class
Magazinethat implements thePrintableinterface. - Create an array of type
Printablethat holds both books and magazines. - Loop through the array and call
print()on each object. - Create a static method
printMagazines(Printable[] printable)in theMagazineclass that prints the titles of magazines only. - Create a static method
printBooks(Printable[] printable)in theBookclass that prints the titles of books only. Use the instanceof operator.
Tip
Since Java 16, you can declare a variable of the target type right after instanceof (pattern matching): if (p instanceof Magazine magazine) { System.out.println(magazine.getTitle()); }. No explicit (Magazine) p cast is needed. Try solving the task both ways.
Video walkthrough → Solution on Patreon →
Task 2. Seasons (enum)
- Create an enum
Seasonwith the seasons:WINTER,SPRING,SUMMER,AUTUMN. - Create a variable holding your favorite season and print all information about it: the constant name (
name()), its ordinal (ordinal()) and the result oftoString(). - Create a method that takes a variable of your enum type. If the value is
SUMMER, print “I love summer”, and so on for every season. Use aswitchstatement. - The enum must have a field with the average temperature of each season.
- Add a constructor that takes the average temperature.
- Create a method
getDescription()that returns “Cold season”. OverridegetDescription()for theSUMMERconstant so that it returns “Warm season”. - Loop over
Season.values()and print every season with its average temperature and description.
Modern switch
Besides the classic switch with break, step 3 can be solved with a switch expression (Java 14+). If all enum constants are listed, no default branch is needed — the compiler checks exhaustiveness for you.
String text = switch (season) {
case WINTER -> "I love winter";
case SPRING -> "I love spring";
case SUMMER -> "I love summer";
case AUTUMN -> "I love autumn";
};
System.out.println(text); Video walkthrough → Solution on Patreon →
Task 3. Clothing
- Create an enum
ClothingSizewith the clothing sizes:XXS,XS,S,M,L.- The enum has a numeric field
euroSize(32, 34, 36, 38, 40) for each size and a constructor that takeseuroSize. - The enum has a method
getDescription()that returns “Adult size”. OverridegetDescription()for theXXSconstant so that it returns “Kids size”.
- The enum has a numeric field
- Create the interfaces
MenClothingwith the methoddressMan()andWomenClothingwith the methoddressWoman(). - Create an abstract class
Clotheswith the fields: size (ClothingSize), price and color. - Create subclasses of
Clothes:TShirt— implementsMenClothingandWomenClothing;Pants— implementsMenClothingandWomenClothing;Skirt— implementsWomenClothing;Tie— implementsMenClothing.
- Create an array containing all types of clothing. Create a class
Atelierwith the methodsdressWoman(Clothes[] clothes)anddressMan(Clothes[] clothes), both taking this array.dressWomanprints all information about women’s clothing,dressMan— about men’s clothing. Select the right items withinstanceof.
Easy to get wrong
An enum constructor is always implicitly private: you cannot declare it public or protected, and you cannot call it with new ClothingSize(32) either. The constants are created once, when the enum class is initialized. Also note the naming convention: enum constants are written in UPPER_CASE, while classes and interfaces use PascalCase.
Task 4. Online Store, Part 2
Continue building the “online store” mini-application (part 1). It should support the following features:
- User authentication: the user enters a login and password from the keyboard (for example, using
Scanner). - Viewing the list of product catalogs.
- Viewing the products of a specific catalog.
- Adding the selected product to the cart.
- Buying the products in the cart.
Create an enum with a value for each of these operations and choose the action with a switch on that enum. Feel free to add your own operations or change anything you like.
More practice on classes, inheritance and polymorphism: Java OOP exercises.
Frequently Asked Questions
What is an interface in Java and how is it different from an abstract class?
A Java interface is a contract: a set of methods a class must implement. Interface fields are always public static final (constants); since Java 8 an interface can have default and static methods, and since Java 9 — private methods. An abstract class, unlike an interface, can hold state (regular fields) and have constructors. The key practical difference: a class can implement any number of interfaces but extend only one class.
Can an enum implement an interface or extend a class?
Implement an interface — yes, for example enum Season implements Printable, and each constant can implement the method in its own way. Extend another class — no: every enum implicitly extends java.lang.Enum, and Java has no multiple class inheritance. You also cannot extend an enum with your own class.
What happens if a class implements two interfaces with the same default method?
The code does not compile until the class overrides that method itself. Inside the override you can explicitly pick an implementation, for example MenClothing.super.info(). If the matching methods are abstract in both interfaces, there is no conflict — a single implementation in the class is enough.
How do you write case labels in a switch on an enum?
Traditionally, the label contains only the constant name: case SUMMER, without the type name. Writing case Season.SUMMER was a compile error before Java 21; since Java 21 the qualified name is allowed. A switch expression (Java 14+) must cover all enum constants or include default, otherwise the code does not compile.
Comments