Java OOP Concepts : Abstraction

Hello coders, I am back with another new article in my Java OOP Concepts article series. The concept that I’m going to elaborate today is called Abstraction.

What is Abstraction?

In Java, Abstraction is the process of hiding certain details from the end-user and showing only the essential information.

How can we achieve Abstraction?

This concept can be implemented in two ways in Java.

1. Using Abstract Classes

When we make a class Abstract in Java, we can’t create objects from those classes.

We can only access the data inside it once some other class inherits it (using extends keyword).

Abstract classes can contain abstract methods. Abstract methods do not have a body. The subclass that inherits this has to provide the body for the class.

Abstract methods can only be declared inside an Abstract class. But abstract classes can hold normal methods as well.

Take a look at these code snippets to get a better idea.

Animal class (Abstract)

abstract class Animal {

    public abstract void speak();

    public void sleep() {
        System.out.println("Sleeping Like an Animal");
    }
}

Cat class

public class Cat extends Animal{

    @Override
    public void speak() {
        System.out.println("Cat says Meow");
    }

}

Abstraction class (Main class)

public class Abstraction {

    public static void main(String[] args) {

        Cat cat = new Cat();
        cat.speak();
    }
}

Here is the console output of this.

2. Using Interfaces

Interfaces are just like abstract classes. Interfaces also hold methods with empty bodies.

A class needs to implement an interface to access the methods inside it (using implements keyword).

The cool thing about interfaces is that a class can implement multiple interfaces at once. But a class can’t extend multiple classes.

You will understand the concept better once you read the following code snippet.

Animal Interface

interface Animal {

    public void speak();

}

Mammal Interface

interface Mammal {

    public void A();

}

Cat class

public class Cat implements Animal, Mammal{

    @Override
    public void speak() {
        System.out.println("Cat says Meow");
    }

    @Override
    public void A() {
        System.out.println("I am a Mammal");
    }
}

Abstraction class (Main class)

public class Abstraction {

    public static void main(String[] args) {

        Cat cat = new Cat();
        cat.speak();
        cat.A();
    }
}

Here is the console output for the above code


Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day. Have a nice day ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: