Abstract Class in Java & When to use It?

What is abstract class and how to implement abstract class in Java?

Being a developer/Automation Engineer you must come across word Abstract class or abstraction. Even it’s one of the favorite topics for interviewers and they try to check your knowledge on abstraction. So this tutorial will help you to understand what is abstract? when to use abstraction or abstract class? and step by step implementation of abstraction in Java.

What is Abstract Class?

An abstract class is a class that is declared with keyword “abstract”. It may or may not be abstract methods. An abstract class cannot be instantiated but they can be subclassed.

Sample abstract Class declaration

package abstractDemo;

public abstract class Base {

}

What is Abstract Method?

In abstract class a method with the abstract keyword that is declared only not implementation (without braces and followed by a semicolon);

package abstractDemo;

public abstract class Base {
	abstract void sum(int a, int b);
	abstract void subtract(int a, int b);

}

When an abstract class is a sub classed, the subclass implements these methods.

Note: Abstract Class can have a mixture of abstract methods and non-abstract methods.

package abstractDemo;

public abstract class Base {
	abstract void sum(int a, int b);
	abstract void subtract(int a, int b);
	public void displayMessage() {
		System.out.println("This is non abstract method");
	}

}

When to use Abstract Class?

Generally, an abstract class is to create base classes that can be extended by subclasses to create a full implementation. Abstract Class is basically used for:

  1. Share Code among several closely related classes.
  2. Common methods are declared and implemented in abstract classes. These methods are used in the subclass.
  3. Abstract methods and fields can be static and non-static fields. This enables you to define methods and that can access and then modify the state of the object in which they belong.

Example: Let’s assume you have to develop a calculator and calculator functionalities (methods) will be used by different vendors with little modification. So it will be a great idea to create an abstract class and declare all the methods in the abstract class. And during each vendor class calculator methods can be implemented as per their need. This is one of the best use cases to understand the abstract class.

Abstract Class Calculator

Step by Step Implementation of Abstract Class:

To explain abstract class, here is the step by step implementation of abstract class and their subclass. It will give a complete understanding. Refer below for step by step implementation and code explanation:

Step 1: Open Java Project in Eclipse and create abstract class:

  • Java should be installed and Eclipse or IntelliJ Idea should be set up. (If Java is not installed then refer our tutorial https://thoughtcoders.com/blog/install-java-and-setup-environment-variable/
  • Java Project should be created
  • Step 1: Open Java Project in Eclipse and create abstract class
Abstract java class
package abstractDemo;

public abstract class Base {
	
}
  • Step 2: Create Abstract Methods: Now you can declare abstract methods within the above abstract class. Abstract methods are a method without implementation.
package abstractDemo;

public abstract class Base {
	abstract void sum(int a, int b);
	abstract void subtract(int a, int b);
	public void displayMessage() {
		System.out.println("This is non abstract method");
	}

}
  • Step 3: Write Subclass (Child Class) and extends abstract Class: To use parent class method we need to inherit parent class by keywords “extends”. Refer below
package abstractDemo;

public class Childclass extends Base {

	public static void main(String[] args) {
		

	}

}
  • Step 4: Implement abstract class methods:
Abstract class methods
  • Step 5: Override annotation used at the time of definition:

public class Childclass extends Base {

package abstractDemo;

public class Childclass extends Base {

	public static void main(String[] args) {
		

	}

	@Override
	void sum(int a, int b) {
				
	}

	@Override
	void subtract(int a, int b) {
				
	}

}
  • Step 6:
package abstractDemo;

public class Childclass extends Base {

	public static void main(String[] args) {
		// Make object of Child Class to use implemented abstract class
		Childclass child = new Childclass();
		child.sum(3,4);
		child.subtract(5,1);
		// method which implemented in Base class
		child.displayMessage();

	}

	@Override
	void sum(int a, int b) {
		int c = a+ b;
		System.out.println("Sum method implmented here: "+c);
		
	}

	@Override
	void subtract(int a, int b) {
		int c = a- b;
		System.out.println("subtract method implmented here: "+c);
		
	}

}

Console Output:

console output abstraction

Brief Summary:

  1. An abstract class is declared with the keyword “abstract”.
  2. Abstract class can’t be instantiated in java.
  3. An abstract class with the private and final method is not possible in Java. As these methods can’t be implemented or used in subclasses.
  4. An abstract class with zero abstract methods is possible.
  5. The static method should have a compulsory implementation available while abstract methods should not have the definition. So the static method not possible in the abstract class.
  6. If all methods of an abstract class are abstract then better to make a class Interface.
  7. The subclass is responsible for the implementation of abstract methods.
  8. Abstract Classes should not be static, final, define public, protected, and private concrete methods.

Hope you enjoyed this article and understand abstract and able to implement it in your framework. To learn more about other topics refer to our next blog post. If you have any doubt then feels free to contact us on query@thoughtcoders.com

Subscribe to our Newsletter:

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact