DEV Community

Cover image for Abstraction in Java: A Complete Guide with Examples
Vinayagam
Vinayagam

Posted on

Abstraction in Java: A Complete Guide with Examples

Introduction

Abstraction is one of the four fundamental principles of Object-Oriented Programming (OOP) in Java. It helps developers focus on what an object does rather than how it does it. By hiding implementation details and exposing only essential features, abstraction makes programs easier to use, maintain, and extend.

For example, when driving a car, we use the steering wheel, brake, and accelerator without knowing the internal working of the engine. Similarly, abstraction in Java hides complex implementation details from the user.

What is Abstraction?

Abstraction is the process of hiding internal implementation details and showing only the necessary functionality to the user.

In Java, abstraction can be achieved using:

  1. Abstract Classes
  2. Interfaces

The main goal of abstraction is to reduce complexity and increase code reusability.

Abstract Class in Java

An abstract class is a class that cannot be instantiated directly. It may contain both abstract methods (methods without a body) and concrete methods (methods with a body).

Example

Animal.java

Dog.java

Main.java

ouput

Top comments (0)