This study guide highlights the most important subjects in Chapter 13. When studying Chapter 13 try to understand at least the concepts presented here, including the examples and figures. Relevant paragraphs: 13.1 - 13.2, 13.5 - 13.6, 13.8, 13.9, 13.10. Please skip other paragraphs.

By defining a class, we actually define a new data type. Beside a class, we can also define a so-called abstract class or an interface to define a new data type.

1. Abstract classes and methods

  • Abstract class

    • An abstract class is a class that cannot be used to create objects, so a class that cannot be instantiated. The same applies to interface. The counterpart is a concrete class, like you’re used to define till now.

    • If a class GeometricObject is an abstract class, it’s defined as public abstract class GeometricObject. The compiler will not accept the statement GeometricObject g = new GeometricObject(), since the keyword new is used to create an object. An abstract class will have a constructor however, which will be invoked by sub class constructors.

    • An abstract class only makes sense if there are two or more (direct or indirect) concrete sub classes somewhere down the hierarchy, specialising the abstract class. An abstract class can have an abstract sub class.

    • An abstract super class is introduced to avoid code duplication; it might not make sense to create an instance of the more generic type (e.g. GeometricObject is used to define common characteristics, but it doesn’t make sense to create an instance of this class without being it a concrete and specialised type of GeometricObject like Triangle, Circle, Rectangle etc.).

  • Abstract methods

    • An abstract class can contain abstract methods, being methods without a body ({}). The body of those methods MUST be defined by concrete sub classes. An abstract method public abstract double getArea(); in class GeometricObject obliges specialisations like Triangle to implement the area calculation. This way it is guaranteed that each and every specific GeometricObject has the ability to compute its area; on the other hand the computation cannot be done in GeometricObject itself, since it’s different for all specific concrete types.

    • Abstract methods are only allowed within abstract classes or interfaces, so not within concrete classes. Why not?

    • Concrete methods are allowed to be part of an abstract class.

    • By definition, all instance methods of an interface are either abstract or provide a default (since Java 8).

    • Both abstract classes and interfaces (since Java 8) can have static methods.

    • All methods in and interface are always public, even if you do not specify a visibility . In an abstract class you can use any visibility keyword.


  • Questions (answers in paragraph 13.2.2)

    • Is it valid to have an abstract method in a concrete class?

    • Can an object be created from an abstract class?

    • Is it valid to have an abstract class without abstract methods?

    • Can a concrete method be overridden by an abstract method?

    • May the super class of an abstract class be concrete?

  • Abstract class as a data type

    • Variables of the abstract class type may be declared (e.g. GeometricObject c = new Circle()).

2. Interfaces

public interface Edible {
  /** Describe how to eat */
  public abstract String howToEat();
  • A super class defines common behavior for related subclasses. An interface can be used to define common behavior for classes (including unrelated classes).

    • For example, suppose you have an interface Edible that defines a method String howToEat() that returns a String describing how to eat a thing (see above). All things or basically all classes that implement this interface then share the behavior that you can ask these things or objects to explain how you can eat the thing (see Edible interface example in Lesson demo in student repository). In this example:

      • All fruit is considered Edible (i.e. the abstract Fruit class and all its subclasses implement the Edible interface).

      • Some animals (e.g. Chicken) are considered Edible, while others are assumed to be not Edible (e.g. Tiger). Hence, only some subclasses of Animal (e.g. Chicken) implement the Edible interface.

  • Java doesn’t support multiple inheritance; a class can only extend ONE other class or abstract class. Why? Since otherwise a class could possibly inherit two or more implementations of the same method. This is restrctive since a Triangle can for example only act as GeometricObject and not as something else. Interfaces offer a solution.

  • An interface is similar to an abstract class. The keyword interface is used instead of class. An interface decouples the implementation of a class from the definition of methods (signatures) it exposes to the outside world. A class can implement one or more interfaces.

  • An interface only contains public and implicitly abstract methods (so without a body {}) (except possible static methods or default methods, to be discussed later)

  • it’s not possible to create an instance of an interface

  • A class can only implement an interface, it CANNOT extend an interface. An interface CAN extend one or even multiple other interfaces.

  • A class that implements an interface is obliged to provide a body for all methods in the interface (unless it’s an abstract class; in that case a concrete sub class MUST implement the method).

  • By defining an interface, we define a data type. Interface names follow the casing convention of normal classes, being Pascal case. Variables of the interface type may be declared (e.g. Drawable d = new Circle() as long as Circle implements the Drawable interface).

  • A class that implements an interface has an Is-a relation with the interface type, e.g. a Circle object is-a Drawable object.

3. The Comparable interface

public interface Comparable<E> {
  public int compareTo(E o);

4. Interfaces vs Abstract classes vs Concrete classes

Table 1. Table comparing different data types
Item Concrete class Abstract class Interface





Valid to instantiate using new?




Abstract methods allowed?



Yes, mandatory

Concrete methods allowed?



No, unless default or static


Not restricted

Not restricted ( abstract methods: all but private )

Everything public


Not restricted

Not restricted

all public static final

5. The Fraction (Rational) class

6. Design guidelines

  • Cohesion

  • Consistency

  • Encapsulation

  • Instance vs Static

  • Inheritance vs Aggregation

  • Interfaces vs Abstract classes