- 1UML Relationships in Java: Association, Aggregation, Composition Explained with Examples
- 2UML Inheritance and Generalization | Java Examples for Beginners
- 3Interfaces vs Abstract Classes in UML with Java Examples
- 4Multiplicity and Navigability in UML – Easy Guide with Real-Life Examples
- 5Constraints and Notes in UML – Beginner-Friendly Guide with Java Examples


- 1Class Diagram in UML | Beginner-Friendly Guide with Examples
- 2Object Diagram in UML | Simple, Visual Examples for Beginners
- 3Component Diagram in UML | Beginner-Friendly Tutorial with Java Examples
- 4Deployment Diagram in UML | Beginner’s Guide with Examples and Java Use Cases
- 5Package Diagram in UML | Java-Focused Tutorial with Real Examples
- 6Composite Structure Diagram in UML | Java Examples & Complete Guide for Beginners

- 1Use Case Diagram in UML | Beginner's Guide with Java and Real-World Examples
- 2Activity Diagram in UML | Java-Centric Beginner's Guide with Real Examples
- 3Sequence Diagram in UML | Step-by-Step Java Guide with Real-World Examples
- 4UML Communication Diagram | Java-Based Tutorial with Real-Life Examples
- 5UML State Machine Diagram | Java-Centric Tutorial with Real Examples
- 6UML Interaction Overview Diagram | Java Tutorial with School-Based Examples
- 7UML Timing Diagram | Java Tutorial with Real-World Timing Examples

- 1UML Relationships in Java: Association, Aggregation, Composition Explained with Examples
- 2UML Inheritance and Generalization | Java Examples for Beginners
- 3Interfaces vs Abstract Classes in UML with Java Examples
- 4Multiplicity and Navigability in UML – Easy Guide with Real-Life Examples
- 5Constraints and Notes in UML – Beginner-Friendly Guide with Java Examples

- 1UML in Software Development Lifecycle (SDLC) – Complete Guide with Examples
- 2How to Create UML Diagrams from Requirements – Step-by-Step with Examples
- 3UML and Agile: A Practical Guide for Beginners
- 4Case Study: UML for an E-commerce Application – Step-by-Step UML Design
- 5UML Best Practices and Common Mistakes – A Beginner’s Guide with Examples


- 1Quiz: UML Concepts – Test Your Understanding of UML Diagrams and Principles
- 2Practical Assignment: UML Modeling – Step-by-Step UML Design Task for Java Beginners
- 3UML Review and Feedback – How to Evaluate and Improve UML Models in Java Projects
- 4UML Certificate of Completion – How to Earn and Use Your Certification
Interfaces and Abstract Classes in UML and Java
Next Topic ⮕Multiplicity and Navigability in UML – Easy Guide with Real-Life Examples
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 Representation of Abstract Classes
Abstract classes in UML are marked by either italicizing the class name or using the stereotype <<abstract>>
.

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

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?
interface NoticeSender {
void send();
}
class SMSNotification implements NoticeSender {
public void send() {
// send SMS
}
}