Abstraction is one of the major features of Object-Oriented Programming. Abstraction in Java is used to show only essential details to the user. Menas hiding an unnecessary internal functionality with users.
For example, Smart mobile applications only show user applications and useful functions like in Youtube watch videos. Youtube does not show to use how internally it’s connected to the server, downloading videos and codes. Users can only interact with the UI of the application.
Definition:- Abstraction is a process of application development to hiding the implementation details from the user. Оnly the user’s useful functionality will be provided to the user. Java Abstraction is achieved by using abstract classes and interfaces.
How to achive Abstraction in Java Example
There are two ways to achieve abstraction in java.
- Abstract class (0 to 100%)
- Interface (Achieve 100% abstraction)
Before seeing the example of Abstraction you have to know about few things.
Let’s see the simple Java abstraction example program code.
The example has a Bank abstract class. Then there are 2 more classes that represent the banks with their interest rate method.
abstract class Bank { abstract int getRateOfInterest(); } class SBI extends Bank { int getRateOfInterest() { return 7; } } class HDFC extends Bank { int getRateOfInterest() { return 8; } } class TestBank { public static void main(String args[]) { Bank b; b = new SBI(); System.out.println("SBI Interest is: " + b.getRateOfInterest() + " %"); b = new HDFC(); System.out.println("HDFC Interest is: " + b.getRateOfInterest() + " %"); } }
What are some real-time examples of Abstraction in Java?
Answer:- Let’s see some example of real-time abstractions:-
- Vehicle (car/bike) – You can do riding a bike or driving a car without knowledge about hows it works.
- ATM Machine – It’s a day used machine. Here you can do cash withdrawal, money transfer, check bank balance, etc. But you don’t know hows internally implementations have done and work.
Types of abstraction
Typically abstraction can be seen in two ways:
- Data abstraction
- Control abstraction
Abstraction with Interface
An abstract class can also use to provide an implementation of the interface. Using an interface forced to implement all methods. See below example:-
interface MainInterface { void Method1(); void Method2(); void Method3(); } abstract class SampleAbsClass implements MainInterface { public void Method4() { System.out.println("I am c"); } } class NormalClass extends SampleAbsClass { @Override public void Method1() { } @Override public void Method2() { } @Override public void Method3() { } } class Example { public static void main(String args[]) { MainInterface mi = new NormalClass(); mi.Method1(); mi.Method2(); mi.Method3(); } }
How to Abstract class having constructor, data member, and methods?
You can use data member, abstract method, method body (non-abstract method), constructor, and even main() method in the abstract class. Like the below example.
abstract class Bike { Bike() { System.out.println("Bike created"); } abstract void run(); void changeGear() { System.out.println("Gear changed"); } } //Creating a Child class inherits Abstract class class Honda extends Bike { void run() { System.out.println("Bike is running safely..."); } } //Creating a Test class which calls abstract and non-abstract methods class BikeAbstraction { public static void main(String args[]) { Bike obj = new Honda(); obj.run(); obj.changeGear(); } }
Do comment if you have any doubts or suggestions on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java abstraction Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.