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");
}
}Leidinysis a general concept covering books, magazines and other publications.abstract classmeans this class is intended for inheritance, not direct object creation.- The abstract method
rodytiTipa()specifies what each concrete publication must do. - The regular method
rodytiBusena()already has code, so subclasses can inherit it unchanged. - This code only declares a class, so it produces no console output yet.
| Element | Property |
|---|---|
| Abstract class | Cannot be instantiated directly; intended for inheritance |
| The abstract method | Has no method body; a concrete subclass supplies the implementation |
| The regular method | Has a body and can be inherited unchanged |
You cannot instantiate an abstract class
Leidinys leidinys = new Leidinys();
// Error: an abstract class cannot be instantiatederror: 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");
}
}Knyga extends Leidinyscreates a concrete publication subclass.- Class
Knygamust provide the abstract method'srodytiTipa()body. - The method body determines what the program does when this method is called on a book object.
- 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();
}
}Tai yra knyga
Leidinį galima pasiskolinti
new Knyga()is allowed becauseKnygais a concrete class, not an abstract one.rodytiTipa()runs the implementation defined in the classKnygaitself.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();
}
}Žurnalas: Mokslas šiandien
- When creating a
Zurnalasobject, its constructor receives the title. super(pavadinimas)passes the value to the abstract class's constructor.Leidinysconstructor stores the shared fieldpavadinimas.- 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();
}
}
}Tai yra knyga
Tai yra žurnalas
- The array uses the common type
Leidinys, so it can hold different subclasses. - The loop knows only the contract defined by the abstract class: the method
rodytiTipa(). - 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 are especially useful for a shared behavior contract between classes that do not necessarily share state.