This tutorial will focus on what is Abstraction in Java. It is one of those topics which is not easier for beginners to understand. But we will make sure to keep things as clear as possible to stay on the same page.
So, let’s begin with the introduction of what is Abstraction in Java.
What is Abstraction in Java?
Abstraction in Java is a process of hiding the background work of a task and showing only the relevant or important features to the user. In abstraction in Java, we try to keep things simple by hiding unnecessary or non-essential things and showing only the essential or relevant things to the user. This way, things are easy for the user.
To better understand the concept of What is Abstraction in Java? let us an example.
Did you ever need to look inside a television to know how it works or how the images are formed on the screen?
The straight answer to this question is No. You never did. Why?
Even the manufacturer of that television knew it wasn’t important to you. All that was important for you were those buttons that he placed on the television panel which you used to control the volume, colour, channels, etc.
It is the best example of What is Abstraction in Java. The manufacturer hid unnecessary features of the television and revealed only the important ones and the same concept of What is Abstraction in Java programming goes for Java.
In Java, we use the abstract keyword to perform abstraction in Java. The abstract keyword is used for classes and methods to make them of abstract nature.
There are a few things to know before we jump into the example:
- The object of an abstract class cannot be created directly.
- An abstract class can be used to extend a class and its properties, methods, and variables can be used in its subclass (inheritance).
- An abstract class can have any number of abstract methods.
- Abstract methods are those methods that do not have a method body and end with a semicolon.
- A subclass that inherits from an abstract class must implement all the abstract methods of the inherited abstract class.
- If a subclass does not implement all the abstract methods of the inherited abstract class, then that subclass also becomes an abstract class and cannot be instantiated.
Let’s see abstraction in Java action using an example.
abstract class Television { int volume = 50; int channelNumber = 34; public abstract void powerOn(); public abstract void powerOff(); void increaseVolume(int amount) { volume = volume + amount; } void decreaseVolume(int amount) { volume = volume + amount; } void gotoChannel(int newChannelNumber) { channelNumber = newChannelNumber; } }
In the above example, there is a class named “Television”. This class is abstract in nature which means it cannot be instantiated directly. By instantiating, we mean creating an object.
We can inherit the Television class to another class and implement all the abstract methods and then the class can be instantiated.
class MyTelevision extends Television { boolean powerStatus = false; public void powerOn() { powerStatus = true; System.out.println("The television is ON"); } public void powerOff() { powerStatus = false; System.out.println("The television is OFF"); } }
The above class extends the functionalities of the Television class. This way all the things that are in the Television class are now part of the MyTelevision class. Now the user will only have to implement the necessary methods i.e. powerOn() and powerOff() and then he will be able to instantiate the class.
class MainClass { public static void main (String agmts[]) { MyTelevision myTv = new MyTelevision(); myTv.powerOn(); System.out.println("Current volume: " + myTv.volume); myTv.increaseVolume(20); System.out.println("New volume: " + myTv.volume); } } /* Output The television is ON Current volume: 50 New volume: 70 */
This way you can create any number of the subclass that will extend the Television class and then you will have to only implement the important required parts. And all the other pre-built things of the abstract class are working for you.
Java provides so many abstract classes which are so much useful for a Java developer. These classes are so big. A developer will just have to extend them and implement only the relevant necessary features and he will save a lot of time and will not have to code those classes from scratch.
I hope now you have a better understanding of What is Abstraction in Java. Thank you for reading.