DEV Community

S Sarumathi
S Sarumathi

Posted on

Abstraction

Abstraction is one of the core concepts of Object-Oriented Programming (OOP) in Java.

Abstraction means hiding the internal implementation details and showing only the essential functionality to the user.

Why Abstraction is Important:

Abstraction helps developers to:

  • Hide complex implementation

  • Improve security

  • Reduce code complexity

  • Increase code reusability

  • Make applications easier to maintain

Program:

abstract class Animal {

    // abstract method
    abstract void sound();

    // normal method
    void sleep() {
        System.out.println("Animal is sleeping");
    }
}

class Dog extends Animal {

    // implementation of abstract method
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {

    public static void main(String[] args) {

        Dog d1 = new Dog();

        d1.sound();
        d1.sleep();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)