JV

Java Fundamentals

19 lessons

Progress0%
1. Introduction to Java
1What is Java?
2. Variables and Data Types
1Primitive Types
3. Control Flow
ConditionalsLoops
4. Methods
Defining MethodsMethod Overloading
5. Object-Oriented Programming
Classes and ObjectsInheritanceInterfaces and Abstract Classes
6. Collections
ArrayList and LinkedListHashMap and HashSet
7. Exception Handling
Checked & Unchecked Exceptionstry-with-resources & Custom Exceptions
8. Generics
Generic Classes & MethodsWildcards & Type Erasure
9. Modern Java
Lambdas & Functional InterfacesStream API & Optional
10. File I/O
java.nio.file APIBuffered I/O & try-with-resources
All Tutorials
JavaObject-Oriented Programming
Lesson 9 of 19 min
Chapter 5 · Lesson 3

Interfaces and Abstract Classes

Interfaces and Abstract Classes in Java

Interfaces An interface defines a contract — a set of method signatures that implementing classes must provide. Since Java 8, interfaces may also have default and static methods.

java
interface Drawable { void draw(); }
class Circle implements Drawable { … }

A class can implement multiple interfaces.

Abstract classes An abstract class cannot be instantiated and may contain abstract methods (no body) that subclasses must implement. It can also contain concrete methods and fields.

java
abstract class Vehicle {
  abstract int maxSpeed();
  void describe() { System.out.println("Speed: " + maxSpeed()); }
}

Polymorphism Both mechanisms support polymorphism — treating objects of different types uniformly through a common supertype:

java
List<Drawable> shapes = List.of(new Circle(), new Square());
shapes.forEach(Drawable::draw);

Key points:

  • Use an interface when unrelated classes should share behaviour.
  • Use an abstract class when related classes share state and some implementation.
  • Interfaces cannot have instance fields (only constants).

Code Examples

Interface and polymorphismjava
interface Shape {
    double area();
    default String describe() {
        return getClass().getSimpleName() + " with area " + String.format("%.2f", area());
    }
}

class Circle implements Shape {
    private double radius;
    Circle(double radius) { this.radius = radius; }
    public double area() { return Math.PI * radius * radius; }
}

class Square implements Shape {
    private double side;
    Square(double side) { this.side = side; }
    public double area() { return side * side; }
}

public class ShapeDemo {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(5), new Square(4) };
        for (Shape s : shapes) {
            System.out.println(s.describe());
        }
    }
}

Both classes implement the Shape interface. The default method is inherited and calls the overridden area() polymorphically.

Abstract classjava
abstract class Employee {
    protected String name;
    protected double baseSalary;

    Employee(String name, double baseSalary) {
        this.name = name;
        this.baseSalary = baseSalary;
    }

    abstract double calculateBonus();

    double totalCompensation() {
        return baseSalary + calculateBonus();
    }
}

class Manager extends Employee {
    Manager(String name, double base) { super(name, base); }
    double calculateBonus() { return baseSalary * 0.20; }
}

class Engineer extends Employee {
    Engineer(String name, double base) { super(name, base); }
    double calculateBonus() { return baseSalary * 0.10; }
}

public class PayrollDemo {
    public static void main(String[] args) {
        Employee[] staff = { new Manager("Alice", 80000), new Engineer("Bob", 70000) };
        for (Employee e : staff) {
            System.out.printf("%s: $%.0f%n", e.name, e.totalCompensation());
        }
    }
}

The abstract class provides shared fields and a concrete totalCompensation() method. Subclasses supply calculateBonus() implementations.

Quick Quiz

1. How many interfaces can a Java class implement?

2. Can an abstract class have concrete (implemented) methods?

Was this lesson helpful?

PreviousNext