Abstraction in Java

HOME

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

  1. Abstract class
  2. Interface

What is an Abstract Class?

A class which is declared as abstract is known as an abstract class.

  1. It can have abstract and non-abstract methods.
  2. It needs to be extended and its method is implemented.
  3. It cannot be instantiated.
  4. It can have constructors and static methods also.
  5. It can have final methods which will force the subclass not to change the body of the method.
  6. An abstract class is a class that contains at least one abstract method.
  7. 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

  1. It reduces the complexity of viewing the things.
  2. Avoids code duplication and increases reusability.
  3. Helps to increase security of an application or program as only important details are provided to the user.

Advertisement

Java Tutorials

Java is a general-purpose programming language that is a concurrent, class-based, and object-oriented language. Java follows the concept of “write once and run anywhere (WORA)” which means that compiled Java code can be run on all different platforms that support Java without the need for recompilation.

Eclipse IDE

Chapter 1 How to Download and Install Eclipse IDE
Chapter 2 How to Clone a project from GitLab using Eclipse
Chapter 3 How to Export Eclipse projects to GitLab

IntelliJ IDE

Chapter 1 How to install IntelliJ on Windows
Chapter 2 How to create a Java project in IntelliJ
Chapter 3 How to Clone a project from GitLab using IntelliJ
Chapter 4 How to Export IntelliJ project to GitLab

Basics of Java

Chapter 1 How to Download & Install Java JDK 11 in Windows
Chapter 2 Data Types and Operators in Java
Chapter 3 Decision Making in Java – If, If Else, Switch, Break, Continue
Chapter 4 Loop Control Statements in Java – For, While, Do While, Enhanched For Loop
Chapter 5 String Manipulation
Chapter 6 Difference between == and equals() method in Java
Chapter 7 Arrays in Java
Chapter 8 Access Modifiers in Java
Chapter 9 ArrayList in Java
Chapter 10 Methods in Java
Chapter 11 Method Overloading in Java
Chapter 12 Constructors in Java   
Chapter 13 This Keyword in Java   
Chapter 14 Static Keyword – Static Variable and Static Method in Java
Chapter 15 Difference between Static Method and Non-Static Method
Chapter 16 How to use Java Lambda expression to create thread via Runnable function
Chapter 17 runAsync and supplyAsync in ComputableFuture in Java8
Chapter 18 HashMap in Java
Chapter 19 Iterators in Java

OOPs Concepts

Chapter 1 Class and Object in Java
Chapter 2 Inheritance in Java
Chapter 3 Encapsulation in Java
Chapter 4 Polymorphism in Java
Chapter 5 Abstraction in Java
Chapter 6 Interface in Java
Chapter 7 Difference between Abstract Class and Interface

Exceptions in Java

Chapter 1 Exception Handling in Java
Chapter 2 Types of Exception in Java
Chapter 3 Flow control in try catch finally in Java
Chapter 4 Multiple Catch Exceptions
Chapter 5 Throw in Java
Chapter 6 Throws in Java

Data Handling (Excel Manipulation)

Chapter 1 How to download and install Apache POI
Chapter 2 How to read Excel in Java
Chapter 3 How to write data in Excel in Java
Chapter 4 How to update existing excel in Java
Chapter 5 How to add Formulas in Excel in Java

Multiple Choice Questions

Chapter 1 Multiple questions on Exception Handling in Java

Java Library

Chapter 1 AssertJ – Fluent Assertions in Java