Table of Contents
1. Introduction
Abstraction
is a concept of exposing only essential details and hiding implementation details. It is one of the essential OOPs concept apart from encapsulation, inheritance and polymorphism. Abstraction retains only information which is most relevant for the specific purpose.
For example:
ArrayList stores objects sequentially as list, and you can use the add() method to add elements to ArrayList, the remove() method to remove elements from it, and the get() method to retrieve elements from the ArrayList. That’s all you need to know to use ArrayList. That’s an abstraction in Java.
If you want to use ArrayList, you don’t need to understand how ArrayList
works internally.
ArrayList
uses abstraction by implementing List
interface and List interface provides all necessary methods to use ArrayList.
so you can declare ArrayList as below:
1 2 3 4 5 6 7 8 9 10 11 |
List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C++"); list.remove("Python"); String python = list.get(1); |
and call add()
, remove()
and get
methods of List interface.
Suppose you realized later that you need to use LinkedList
rather than ArrayList
as you need to perform more add and remove operations rather than access operations. You can go through ArrayList vs LinkedList to understand the differences between them.
You just need to change highlighted line.
1 2 3 4 5 6 7 8 9 10 |
List<String> list = new LinkedList<>(); list.add("Java"); list.add("Python"); list.add("C++"); list.remove("Python"); String python = list.get(1); |
ArrayList and LinkedList use abstraction by implementing a List interface. That’s why we could replace ArrayList with LinkedList in the above example.
Let’s take another real-life example.
When you search for any text on Google, you just type text in the text area and click on the search button. You might not be aware of what happens behind the scenes and how the google search algorithm works.
2. Ways to achieve abstraction
You can achieve abstraction using two ways in java.
- Abstract class(0 to 100% abstraction)
- Interface (100% abstraction)
2.1 Abstract Class in Java
An abstract class is the class which is declared abstract and can have abstract or non abstract methods
. Â It should be extended by child class and should implement abstract method.
Abstract method in java:
Abstract method is the method which do not have implementation i.e. it does not have any body.
1 2 3 4 5 |
abstract class Shape{ public abstract double calculateArea(); // No body for calculatedArea method } |
2.2 Interface in Java
Interface is generally used to provide contract for class to implement. Interface do not have implementation of any method.A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of signing a contract, you agree that if you implement this interface, then you have to use its methods.It is just a pattern, it can not do anything itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.arpit.java2blog; interface Paintable { void paint(); } public class Room implements Paintable{ @Override public void paint() { System.out.println("Painting the room"); } } |
3. Abstraction example
Let’s say we have Sport
as interface. Now, its implementation will be provided by classes called Cricket
and Football
. In a real scenario, the end user will not be aware of the implementation class, and the object of the implementation class can be provided by the factory method. Factory method can be used to create an object of implementation class based on some criterion.
Let’s create an interface called Sport.java
:
1 2 3 4 5 6 7 8 |
package com.arpit.java2blog; public interface Sport { void play(); } |
Create a class named Cricket.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.arpit.java2blog; public class Cricket implements Sport { @Override public void play() { System.out.println("Playing cricket"); } } |
Create a class named Football.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.arpit.java2blog; public class Football implements Sport { @Override public void play() { System.out.println("Playing football"); } } |
Create a main class named SportInterfaceMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.arpit.java2blog; public class SportInterfaceMain { public static void main(String[] args) { // In real scenario, you will get concrete object using getSport() factory method Sport sport=new Cricket(); sport.play(); System.out.println("================="); // You can easily change implementation sport=new Football(); sport.play(); } } |
When you run above program, you will get below output:
=================
Playing football
4. Abstraction vs Encapsulation
Encapsulation is process of binding related data(variables and methods) into a class.We can achieve encapsulation using access modifers such as public, private, protected.
Abstraction is more of design level concept and helps you to provide only essential details and hiding implementation details. We can achieve abstraction by abstract class
and interface
.
5. Conclusion
You have learnt about What is abstraction, how to achieve abstraction with help of example and difference between abstraction and encapsulation.
That’s all about abstraction
in java.