Post

IV. Abstraction

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

IV. Abstraction

A. Abstraction

1. What is an Abstract Class?

An abstract class in Java is a class that cannot be instantiated on its own and is meant to be extended by other classes.

It may include:

  • Abstract methods (methods without a body)
  • Non-abstract methods (with implementation)
  • Fields and constructors
  • Static and final methods

It provides a base for subclasses to build upon.

2. Declaring an Abstract Class

1
2
3
4
5
6
7
abstract class Animal {
    abstract void speak(); // abstract method

    void breathe() {
        System.out.println("Breathing...");
    }
}

Any class with at least one abstract method must be declared abstract.

3. Creating a Subclass

1
2
3
4
5
6
class Dog extends Animal {
    @Override
    void speak() {
        System.out.println("Bark");
    }
}

Subclasses must implement all abstract methods, unless they are also declared abstract.

4. Key Features

FeatureAbstract Class
Can be instantiatedNo
Can have constructorsYes
Can contain fieldsYes
Can have method bodiesYes (both abstract and concrete methods)
Supports inheritanceSingle class inheritance

5. Use Case

Use an abstract class when:

  • You want to provide common functionality to subclasses
  • You want to partially define a class and force subclasses to complete it

6. Example: Template Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
abstract class Payment {
    void process() {
        authenticate();
        pay();
        receipt();
    }

    void authenticate() {
        System.out.println("Authenticating...");
    }

    abstract void pay();

    void receipt() {
        System.out.println("Transaction complete.");
    }
}

class CreditCardPayment extends Payment {
    void pay() {
        System.out.println("Paid by credit card");
    }
}
1
2
3
4
5
6
public class Main {
    public static void main(String[] args) {
        Payment payment = new CreditCardPayment();
        payment.process();
    }
}

This is an example of the Template Method Pattern.

7. Abstract Class vs Interface

FeatureAbstract ClassInterface
Multiple inheritanceNoYes
Method typesBoth abstract and concreteAbstract, default, static
FieldsVariables allowedOnly constants (public static final)
ConstructorsYesNo
Use caseShared base with some logicCommon capability contract

8. Abstract vs Concrete Class

TypeCan instantiate?Has abstract methods?Has method bodies?
Abstract classNoOptionalYes
Concrete classYesNoYes

B. Interface

1. What is an Interface?

An interface in Java is a reference type, similar to a class, that can contain:

  • Abstract method signatures (without implementation)
  • Default methods (since Java 8)
  • Static methods
  • Constants (implicitly public, static, and final)

Interfaces are used to define a contract that implementing classes must follow.

2. Why Use Interfaces?

  • To achieve abstraction and multiple inheritance
  • To define a standard set of behaviors across unrelated classes
  • To support loose coupling and flexible architecture

3. Declaring an Interface

1
2
3
public interface Animal {
    void speak(); // abstract method
}

4. Implementing an Interface

1
2
3
4
5
public class Dog implements Animal {
    public void speak() {
        System.out.println("Bark");
    }
}

A class must implement all methods declared in the interface, unless the class is abstract.

5. Multiple Interfaces

A class can implement multiple interfaces, unlike class inheritance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck flying");
    }

    public void swim() {
        System.out.println("Duck swimming");
    }
}

6. Interface Features by Java Version

FeatureAvailable SinceDescription
Abstract methodsJava 1Method signatures without body
Default methodsJava 8Methods with body; allow backward compatibility
Static methodsJava 8Utility methods within interfaces
Private methodsJava 9Helper methods inside default/static methods

7. Default Methods

1
2
3
4
5
interface Logger {
    default void log(String msg) {
        System.out.println("LOG: " + msg);
    }
}

A class implementing Logger can use or override log().

8. Constants in Interfaces

All fields in interfaces are implicitly:

  • public
  • static
  • final
1
2
3
interface Config {
    int MAX_USERS = 100;
}

9. Interface vs Abstract Class

FeatureInterfaceAbstract Class
InheritanceMultiple (via implements)Single (via extends)
MethodsAbstract, default, staticAbstract and concrete
FieldsConstants onlyVariables and constants
ConstructorNoYes
Use CaseCapability/ContractCommon base class with logic

10. Real-world Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
interface Payment {
    void pay(double amount);
}

class CreditCardPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " by credit card");
    }
}

class PayPalPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " using PayPal");
    }
}

class Checkout {
    void completePayment(Payment method, double amount) {
        method.pay(amount); // Polymorphism via interface
    }
}
This post is licensed under CC BY 4.0 by the author.