OBJECT-ORIENTED PROGRAMMING

Java abstraction

Abstraction hides unnecessary implementation details and exposes only an object's essential capabilities. In Java, it can be implemented with abstract classes, abstract methods and interfaces.

What does abstraction provide?

  • defines what related objects must be able to do;
  • hides how a particular action is implemented;
  • centralizes code shared by subclasses;
  • reduces dependence on specific implementations;
  • makes polymorphism and new object types easier to use.

abstract classes and methods

abstract is a non-access modifier. It can mark a class or a method:

abstract class Leidinys {
    public abstract void rodytiTipa();

    public void rodytiBusena() {
        System.out.println("Leidinį galima pasiskolinti");
    }
}
  1. Leidinys is a general concept covering books, magazines and other publications.
  2. abstract class means this class is intended for inheritance, not direct object creation.
  3. The abstract method rodytiTipa() specifies what each concrete publication must do.
  4. The regular method rodytiBusena() already has code, so subclasses can inherit it unchanged.
  5. This code only declares a class, so it produces no console output yet.
Members of an abstract class
ElementProperty
Abstract classCannot be instantiated directly; intended for inheritance
The abstract methodHas no method body; a concrete subclass supplies the implementation
The regular methodHas a body and can be inherited unchanged

You cannot instantiate an abstract class

Leidinys leidinys = new Leidinys();
// Error: an abstract class cannot be instantiated
Compiler error

error: Leidinys is abstract; cannot be instantiated

Leidinys describes only a general idea. To create an object, first define a concrete subclass, such as Knyga.

Implementing an abstract method

A concrete subclass inherits the abstract class and must implement its abstract methods:

abstract class Leidinys {
    public abstract void rodytiTipa();

    public void rodytiBusena() {
        System.out.println("Leidinį galima pasiskolinti");
    }
}

class Knyga extends Leidinys {
    @Override
    public void rodytiTipa() {
        System.out.println("Tai yra knyga");
    }
}
  1. Knyga extends Leidinys creates a concrete publication subclass.
  2. Class Knyga must provide the abstract method's rodytiTipa() body.
  3. The method body determines what the program does when this method is called on a book object.
  4. This block does not call the method yet, so it produces no console output.

If a subclass does not implement all inherited abstract methods, it must also be marked abstract.

Using a concrete object

public class Main {
    public static void main(String[] args) {
        Knyga knyga = new Knyga();

        knyga.rodytiTipa();
        knyga.rodytiBusena();
    }
}
Console output

Tai yra knyga
Leidinį galima pasiskolinti

  1. new Knyga() is allowed because Knyga is a concrete class, not an abstract one.
  2. rodytiTipa() runs the implementation defined in the class Knyga itself.
  3. rodytiBusena() runs the ready-made method inherited from the abstract class.

An abstract class can have a constructor

Although an abstract class cannot be instantiated directly, its constructor initializes shared subclass state:

abstract class Leidinys {
    protected String pavadinimas;

    Leidinys(String pavadinimas) {
        this.pavadinimas = pavadinimas;
    }

    public abstract void rodytiInformacija();
}

class Zurnalas extends Leidinys {
    Zurnalas(String pavadinimas) {
        super(pavadinimas);
    }

    @Override
    public void rodytiInformacija() {
        System.out.println("Žurnalas: " + pavadinimas);
    }

    public static void main(String[] args) {
        Zurnalas zurnalas = new Zurnalas("Mokslas šiandien");
        zurnalas.rodytiInformacija();
    }
}
Console output

Žurnalas: Mokslas šiandien

  1. When creating a Zurnalas object, its constructor receives the title.
  2. super(pavadinimas) passes the value to the abstract class's constructor.
  3. Leidinys constructor stores the shared field pavadinimas.
  4. The subclass method uses the inherited title and prints information.

Abstraction and polymorphism

An abstract class can provide a common type for different concrete objects:

abstract class Leidinys {
    public abstract void rodytiTipa();
}

class Knyga extends Leidinys {
    public void rodytiTipa() {
        System.out.println("Tai yra knyga");
    }
}

class Zurnalas extends Leidinys {
    public void rodytiTipa() {
        System.out.println("Tai yra žurnalas");
    }
}

public class Main {
    public static void main(String[] args) {
        Leidinys[] leidiniai = {
            new Knyga(),
            new Zurnalas()
        };

        for (Leidinys leidinys : leidiniai) {
            leidinys.rodytiTipa();
        }
    }
}
Console output

Tai yra knyga
Tai yra žurnalas

  1. The array uses the common type Leidinys, so it can hold different subclasses.
  2. The loop knows only the contract defined by the abstract class: the method rodytiTipa().
  3. Each actual object supplies its own implementation of this method.

When should you choose an abstract class?

  • When related subclasses need shared fields or a constructor.
  • When some methods share an implementation and subclasses must supply others.
  • When you want to prevent instantiation of an overly general base class.
  • When classes form a clear inheritance hierarchy.
Interfaces can also implement abstraction.

Interfaces are especially useful for a shared behavior contract between classes that do not necessarily share state.