Java OOP – Chapter 6: Abstraction in Java
T
Tuan Beo

Java OOP – Chapter 6: Abstraction in Java

In this lesson, you’ll learn about Abstraction, a core principle of Object-Oriented Programming. Abstraction helps hide internal implementation details and exposes only the essential behavior of an object.

What is Abstraction?

Abstraction means showing only relevant information and hiding the complexity. It’s like using a remote control: you press a button to change channels but don’t care how the signal transmission works internally.

In Java, abstraction is implemented using:

  1. Abstract classes

  2. Interfaces

1. Abstract Classes

An abstract class:

  • Can have both implemented and abstract (unimplemented) methods.

  • Cannot be instantiated directly.

  • Is meant to be extended by subclasses.

abstract class Animal {
    abstract void makeSound(); // Abstract method

    void eat() {
        System.out.println("Eating...");
    }
}

A subclass must override the abstract method:

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks");
    }
}

Usage:

Animal a = new Dog();
a.makeSound(); // Dog barks
a.eat();       // Eating...

2. Interfaces

An interface defines a contract with method signatures only. A class that implements an interface must provide all its methods.

interface Vehicle {
    void start();
}

Implemented in a class:

class Car implements Vehicle {
    public void start() {
        System.out.println("Car started");
    }
}

You can also define default methods in interfaces (since Java 8).

Feature

Abstract Class

Interface

Can contain fields

Yes

No (only constants)

Can contain method bodies

Yes

Since Java 8 (default methods)

Inheritance

extends (one only)

implements (multiple allowed)

Use case

Partial abstraction

Full abstraction

Summary

  • Abstraction hides details and shows only essentials.

  • Use abstract classes when you want to share some base logic.

  • Use interfaces when defining pure contracts that multiple classes can implement.

Assignment: Lesson 6 – Abstraction

Objective:

Practice creating and using abstract classes and interfaces.

Part 1: Conceptual Questions

Answer these briefly:

  1. What is abstraction in Java?

  2. What’s the difference between an abstract class and an interface?

  3. Can you create an object of an abstract class?

Part 2: Coding Task

Step 1 – Abstract Class:

Create an abstract class Employee:

  • Field: name (String)

  • Method: work() – abstract

  • Method: showName() – prints the employee's name

Create a subclass Developer that:

  • Implements the work() method to print "Writing code".

Step 2 – Interface:

Create an interface Playable with a method play().
Create classes Guitar and Piano that implement Playable.

In main():

  • Create and use a Developer object.

  • Create and use Playable references to Guitar and Piano.

Github: https://github.com/tuanbeovnn/java-bootcamp/tree/java-bootcamp-oop-chapter6

Comments