Interfaces, enum ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-09-29

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

  1. Define an interface Printable with a method void print().
  2. Define a class Book that implements the Printable interface.
  3. Define a class Magazine that implements the Printable interface.
  4. Create an array of type Printable that holds both books and magazines.
  5. Loop through the array and call print() on each object.
  6. Create a static method printMagazines(Printable[] printable) in the Magazine class that prints the titles of magazines only.
  7. Create a static method printBooks(Printable[] printable) in the Book class 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)

  1. Create an enum Season with the seasons: WINTER, SPRING, SUMMER, AUTUMN.
  2. Create a variable holding your favorite season and print all information about it: the constant name (name()), its ordinal (ordinal()) and the result of toString().
  3. 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 a switch statement.
  4. The enum must have a field with the average temperature of each season.
  5. Add a constructor that takes the average temperature.
  6. Create a method getDescription() that returns “Cold season”. Override getDescription() for the SUMMER constant so that it returns “Warm season”.
  7. 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

  1. Create an enum ClothingSize with 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 takes euroSize.
    • The enum has a method getDescription() that returns “Adult size”. Override getDescription() for the XXS constant so that it returns “Kids size”.
  2. Create the interfaces MenClothing with the method dressMan() and WomenClothing with the method dressWoman().
  3. Create an abstract class Clothes with the fields: size (ClothingSize), price and color.
  4. Create subclasses of Clothes:
    • TShirt — implements MenClothing and WomenClothing;
    • Pants — implements MenClothing and WomenClothing;
    • Skirt — implements WomenClothing;
    • Tie — implements MenClothing.
  5. Create an array containing all types of clothing. Create a class Atelier with the methods dressWoman(Clothes[] clothes) and dressMan(Clothes[] clothes), both taking this array. dressWoman prints all information about women’s clothing, dressMan — about men’s clothing. Select the right items with instanceof.

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.

Solution on Patreon →

Task 4. Online Store, Part 2

Continue building the “online store” mini-application (part 1). It should support the following features:

  1. User authentication: the user enters a login and password from the keyboard (for example, using Scanner).
  2. Viewing the list of product catalogs.
  3. Viewing the products of a specific catalog.
  4. Adding the selected product to the cart.
  5. 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.

Solution on Patreon →

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

Please log in or register to have a possibility to add comment.