In this tutorial, I’ll explain about abstraction in Java.
What is Abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
There are two ways to achieve abstraction in java
- Abstract class
- Interface
What is an Abstract Class?
A class which is declared as abstract is known as an abstract class.
- It can have abstract and non-abstract methods.
- It needs to be extended and its method is implemented.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
- An abstract class is a class that contains at least one abstract method.
- When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
Example of Abstract Class
abstract class Test
{
}
What is Abstract Method?
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract int test(int a, int b);
Below is an example of abstraction.
Abstract Class
//Abstract Class
public abstract class Bank {
// Abstract method
abstract void getRateOfInterest();
}
SubClass1
//Subclass (inherit from Bank)
public class SBI extends Bank {
// The body of getRateOfInterest is declared here
public void getRateOfInterest() {
System.out.println("Interest Rate of SBI Bank :" + 5.3);
}
}
SubClass2
//Subclass (inherit from Bank)
public class BOI extends Bank {
// The body of getRateOfInterest is declared here
public void getRateOfInterest() {
System.out.println("Interest Rate of BOI Bank :" + 4.1);
}
}
Test Class
public class AbstractionTest {
public static void main(String[] args) {
Bank bank1 = new SBI();
bank1.getRateOfInterest();
Bank bank2 = new BOI();
bank2.getRateOfInterest();
}
}
Output
Interest Rate of SBI Bank :5.3
Interest Rate of BOI Bank :4.1
Abstract class can have a data member, constructor, abstract method, non abstract method and even main() method.
Below is an example where the abstract class has both abstract method as well as non abstract method.
public abstract class Bike { // abstract class
int x = 25; // Instance Variable
Bike() // Constructor
{
int y = 10;
System.out.println("Value of y :" + y);
System.out.println("Bike is created");
}
abstract void run(); // abstract method
void change_gear() // non abstract method
{
System.out.println("Gear is changed");
}
}
SubClass
public class Trek extends Bike {
void run() {
System.out.println("Bike is running");
}
}
Test Class
public class AbstractionDemoTest {
public static void main(String[] args) {
Bike bike = new Trek();
bike.run();
bike.change_gear();
System.out.println("Value of x :" + bike.x);
}
}
Output
Value of y :10
Bike is created
Bike is running
Gear is changed
Value of x :25
Advantages of Abstraction
- It reduces the complexity of viewing the things.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only important details are provided to the user.