In this tutorial, you will discover the insights about the concept of Java abstract class. There are follow up examples to get into the thick and thin of what the abstraction does.
The major topics covered are:
1. Introduction to abstraction
2. Abstract Class
3. Abstract methods
4. Examples
Java Abstract Class Overview with Examples
Abstract class in Java is like a template for functions that have definition outside. However, unlike the interfaces, it can also have some methods that could have code inside.
What does ABSTRACTION imply?
In object-oriented programming (OOP), abstraction happens to be one of the essential features, be it Java or any other OOP language. It is the strategy or process of presenting to the user only functioning details while hiding the implementation details of the structure. For example, when you browse the Internet, you are only concerned with the address or domain name of a particular web page. Once you enter the address, your browser has to fetch details and interact with servers across the globe. It is not for the end-user to choose how the protocol of communication takes place between the browser and the servers. Coming back to Java, abstraction allows the programmer to hide specific details from the user while mentioning others. The user only has information about what the object does but does not know how it happens. We can implement Java abstraction with the following two concepts:
• Abstract Classes
• Interfaces in Java
We will talk about Abstract Classes in this tutorial.
Abstract Class
An abstract class is the one that has its functions defined in other classes. Hence, we call it Abstract, which works as a template.
The difference between a regular class and an abstract class arises with the keyword “abstract.” To define a class abstract, the programmer should use the abstract keyword before declaring the class.
We can’t instantiate an abstract class. It means we can’t make objects for an abstract class.
If you have to consume the functionality of the abstract class, then get another class extending it. You can attain this behavior by using the “extends” keyword. We term this process as inheriting.
The inheriting class should provide proper implementation details to all the abstract methods in the abstract class.
An abstract class can have regular as well as abstract methods. An abstract method cannot be declared without an abstract class, while the reverse is possible.
The convention for class variables is to be set to private while we can skip it if we want. The inheriting class can have its own set of methods, but it should define a body for all abstract methods in the abstract class.
Abstract Methods
An abstract method is defined within an abstract class when the programmer wants the class to contain a method but does not want to provide real implementation details for it.
You should write the abstract keyword before the method to define any method abstract. Also, you can declare an abstract method in the abstract class. However, the inheriting class must have its definition.
By declaring, we mean specifying just the signature of the method. The body of the method or the definition is available in the extending class.
To define an abstract class, after the signature of the method, we put a semi-colon (;), instead of parenthesis.
Abstraction Examples
In this section, we’ve brought up several examples to demonstrate how abstraction works in Java. You can use the code given here and practice from Java command-line or using the Eclipse IDE.
Example-I
So, let’s create a Java abstract class. It has one regular and one abstract method.
abstract class AbstractClass { private String a; int b; AbstractClass(String label, int num) { a = label; b = num; } String getLabel() { return a; } abstract int getNumber(); }
In the above snippet, the AbstractClass has getNumber() as its abstract method while getName is a standard method. You can’t instantiate an object of this class. Therefore, you should create a concrete class that will inherit the abstract class.
class ConcreteClass extends AbstractClass { ConcreteClass(String label, int num) { super(label, num); } int getNumber() { return b; } }
Now, we can use the methods of AbstractClass after instantiating the ConcreteClass.
If you see, the ConcreteClass constructor contains the super keyword. It calls AbstractClass constructor with the following parameters:
public class Main { public static void main(String args[]) { ConcreteClass obj = new ConcreteClass("AJ", 1); System.out.println( obj.getNumber() ); System.out.println( obj.getLabel() ); } }
Here is the consolidated code:
abstract class AbstractClass { private String a; int b; AbstractClass(String label, int num) { a = label; b = num; } String getLabel() { return a; } abstract int getNumber(); } class ConcreteClass extends AbstractClass { ConcreteClass(String label, int num) { super(label, num); } int getNumber() { return b; } } public class Main { public static void main(String args[]) { ConcreteClass obj = new ConcreteClass("AJ", 1); System.out.println( obj.getNumber() ); System.out.println( obj.getLabel() ); } }
After running the above Java code, you would see the output as shown below:
1 AJ
Example-II
Check out another example to get more clarity. It considers one abstract class, Area and two other subclasses extending it. It surely would help you visualize the need for abstraction:
abstract class Area { String name = ""; Area(String num) { name = num; } abstract int getArea(); } class Rectangle extends Area { int length = 0; int width = 0; Rectangle(String num, int l, int w) { super(num); length = l; width = w; } int getArea() { return length*width; } } class Circle extends Area { int radius = 0; Circle(String num, int r) { super(num); radius = r; } int getArea() { return (int) (3.14*radius*radius); } } public class Main { public static void main(String args[]) { Rectangle obj = new Rectangle("Rectangle", 1, 2); System.out.println( obj.name ); System.out.println( obj.getArea() ); Circle obj1 = new Circle( "Circle", 2 ); System.out.println( obj1.name ); System.out.println( obj1.getArea() ); } }
After running the above Java code, you would see the output as shown below:
Rectangle 2 Circle 12
We hope this tutorial on Java abstract class would have helped you figure out how the abstraction works in Java. Please do practice with the examples to get full clarity.