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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s