Interfaces and Abstract Classes in UML and Java

Introduction

In object-oriented programming and UML modeling, two foundational building blocks often spark confusion: interfaces and abstract classes. While they may seem similar—both allow you to define reusable contracts—they serve very different roles in Java and UML design.

This tutorial demystifies interfaces and abstract classes with the help of relatable examples, UML diagrams, and Java code. You’ll understand when to use each, how they are represented in UML, and how they map to Java structures.

What is an Interface?

An interface in Java is a contract that a class agrees to fulfill. It only declares what should be done, but not how. Interfaces are great for defining capabilities shared by unrelated classes.

Java Interface Example

interface ExamTaker {
    void takeExam();
}

class Student implements ExamTaker {
    public void takeExam() {
        System.out.println("Student is taking the exam.");
    }
}

Here, Student agrees to implement the behavior defined by the ExamTaker interface. The interface doesn't care how the student takes the exam—just that the method takeExam() exists.

What is an Abstract Class?

An abstract class is a partially defined class. It may contain fully implemented methods, as well as abstract methods that subclasses must implement. It is used when you want to share common code among closely related classes.

Java Abstract Class Example

abstract class Person {
    String name;

    void speak() {
        System.out.println(name + " is speaking.");
    }

    abstract void showRole();
}

class Teacher extends Person {
    void showRole() {
        System.out.println(name + " is a teacher.");
    }
}

Person provides a base structure for subclasses like Teacher, which must implement the showRole() method.

UML Representation of Interfaces

In UML, interfaces are shown as classes with the stereotype <<interface>> or using a lollipop notation (a circle attached to a class).

UML Interface Example with Student and ExamTaker

UML Representation of Abstract Classes

Abstract classes in UML are marked by either italicizing the class name or using the stereotype <<abstract>>.

UML Abstract Class Example with Person and Teacher

Differences Between Interface and Abstract Class

Aspect Interface Abstract Class
Methods Only abstract methods (until Java 8 added default/static) Both abstract and concrete methods
Multiple Inheritance Supported Not supported
Constructors Not allowed Allowed
Field Declarations Public static final only Instance variables with any access modifier
UML Notation <<interface>> or lollipop Italic class name or <<abstract>>

When to Use an Interface

  • When unrelated classes need to share a contract
  • To represent a capability (e.g., Runnable, Comparable)
  • When you need multiple inheritance

Example: Different Roles Implementing Same Interface

interface Payable {
    void processPayment();
}

class Student implements Payable {
    public void processPayment() {
        System.out.println("Student pays tuition.");
    }
}

class Teacher implements Payable {
    public void processPayment() {
        System.out.println("Teacher receives salary.");
    }
}

Though Student and Teacher are different, both agree to fulfill the Payable contract.

When to Use an Abstract Class

  • When classes share common state or behavior
  • When you need a base implementation with optional overrides
  • When inheritance is preferred over composition

Example: Base Class with Shared Methods

abstract class Employee {
    String name;

    void clockIn() {
        System.out.println(name + " has clocked in.");
    }

    abstract void doWork();
}

class Admin extends Employee {
    void doWork() {
        System.out.println(name + " is managing records.");
    }
}

Combining Interfaces and Abstract Classes

Often, you'll use both in a layered design. The abstract class provides core logic; interfaces define optional capabilities.

Example

interface Login {
    void authenticate();
}

abstract class Person {
    String name;
    abstract void getRole();
}

class Student extends Person implements Login {
    void getRole() {
        System.out.println("I am a student.");
    }

    public void authenticate() {
        System.out.println("Student logged in.");
    }
}

UML Diagram for Combined Usage

UML diagram showing class implementing interface and extending abstract class

Real-Life Scenario: School System

  • Interface: Examinable – implemented by both students and teachers (e.g., teachers can be evaluated too).
  • Abstract Class: Person – defines common structure and behavior.

Key UML Symbols Recap

  • Interface: Dashed arrow with open triangle (Realization)
  • Abstract Class: Solid arrow with open triangle (Generalization)

Conclusion

Interfaces and abstract classes aren't competitors—they are collaborators in great design. Interfaces allow flexible, contract-based architecture, while abstract classes support structured, reusable inheritance.

As a rule of thumb: Use interfaces to define “what” should be done, and abstract classes to define “how” things are partially done.

With UML, you can model these constructs visually to ensure that your design is clear, extensible, and grounded in real-world logic.

QUIZ

Question 1:What is the primary difference between an interface and an abstract class?

Question 2:An abstract class can contain both abstract and non-abstract methods.

Question 3:Which of the following are true about interfaces?

Question 4:Which is the best choice when you want to define a base class with common attributes and some methods implemented?

Question 5:Interfaces are better suited for modeling capabilities like 'Printable' or 'Savable' than for sharing common data fields.

Question 6:In a school system, which of the following are reasonable examples of using interfaces or abstract classes?

Question 7:What happens if a class implements an interface but does not provide implementations for all its methods?

Question 8:An interface can extend another interface.

Question 9:Which of the following are correct about abstract classes?

Question 10:Consider:
interface NoticeSender {
  void send();
}
class SMSNotification implements NoticeSender {
  public void send() {
    // send SMS
  }
}
What does this code illustrate?