Abstraction in Java
Abstraction is another important OOP’s principle in Java. It is the process of hiding internal implementation details from the user and providing only necessary functionality to the users. It removes all non-essential things and shows only important things to users.
Realtime Examples of Abstraction in Java
1. Let’s first take ATM machine as a real-time example. We all use an ATM machine for cash withdrawal, money transfer, retrieve min-statement, etc in our daily life. But we don’t know internally what things are happening inside ATM machine when you insert ATM card for performing any kind of operations.
How to achieve Abstraction in Java?
There are two ways to achieve abstraction in java. They are as follows:
Abstract Class in Java
An abstract class is a class, which is declared with abstract keyword. It is just like a normal class but has two differences.
1. We cannot create an object of this class. Only objects of its non-abstract (or concrete) sub-classes can be created.
2. It can have zero or more abstract methods which are not allowed in a non-abstract class (concrete class).
1. Abstract is a non-access modifier in java which is applicable for classes, interfaces, methods, and inner classes. It represents an incomplete class which depends on subclasses for its implementation. Creating subclass is compulsory for abstract class.
2. A non-abstract class is sometimes called a concrete class.
3. An abstract concept is not applicable to variables.
Abstract Method in Java
A method which is declared with abstract modifier and has no implementation (means no body) is called an abstract method in java. It does not contain any body. It has simply a signature declaration followed by a semicolon. It has the following general form as given below.
abstract type MethodName(arguments); // No body
1. Abstract method cannot be static.
2. It cannot be private because the abstract method must be implemented in the subclass. If we declare it as private, we cannot implement it from outside the class.
When to Use Abstract class & Abstract method in Java?
Uses of abstract method in Java:
Use of abstract class in Java:
1. An abstract class can be used when we need to share the same method to all non-abstract sub classes with their own specific implementations.
Let’s understand these concepts with a suitable example program.
When we write anything in a class, it is applicable to all its objects. If a method is defined in a class, it is available to all objects of a class. For example, suppose a class MyTest that contains a method calculate() that calculates addition of a given two numbers.
If you create three objects to this class, all three objects will get a copy of this method and from any object, we can call this method. Here, the requirement of all objects is the same i.e, to calculate addition of two numbers.
Now, our requirement is changed. We want that first object calculates addition, second object calculates subtraction, and the third object calculates multiplication. Since calculate() method has to perform three different tasks depending on the object. In such a case, how will write calculate() method in MyTest?
If we declare three methods like add(), sub(), and multiply() in MyTest, all three methods will be available for all three objects which are not advisable. To solve this problem, we will use an abstract class and abstract method. We will declare calculate() method as abstract, it will not has any body within it.
Now, we will derive three subclasses like Addition, Subtraction, and Multiplication from class MyTest wherein each subclass, we will provide a body for calculate() method so that it can calculate add, subtract, and multiply.
In this way, the same abstract method will be implemented in three subclasses as per the requirement of objects and can perform different tasks. We can also create objects of subclasses and respective method can be called using these objects. The hierarchy is shown in the below figure.
Let’s us create a program where abstract class MyTest has one abstract method which has various implementation in sub classes.
Program source code 1:
As you can see in the above example, the requirement of every object has been fulfilled.
Features of Abstract class in Java
There are following important features of abstract class in Java.
1. Abstract class is not a pure abstraction in java.
2. In Java, object creation is not possible for an abstract class because It is partially implemented class, not fully implemented class.
3. It can be abstract even without any abstract method.
5. Abstract class allows to define private, final, static and concrete methods. Everything is possible to define in an abstract class as per application requirement.
8. It can implement one or more interfaces in java.
Rules of Abstract class in Java
1. Class must be declared with abstract keyword to make an abstract class.
4. To use methods declared in an abstract class, the abstract class must be extended by an ordinary class and must implement (override) all abstract methods in that ordinary class.
5. If a new abstract method is added in the abstract class, all non-abstract subclass which extends that abstract class, must implement the newly added abstract method. If it does not implement all the abstract method, the class must be declared as abstract.
Java Abstract Class Example Programs
Let us make a program where we will try to create an object of abstract class but the compiler will show a compile-time error.
Program source code 2:
Let us make a program where an abstract class Hello contains both abstract method and instance method. The abstract method “msg2” will be implemented in Test class that extends a class Hello.
Program source code 3:
In this example program, we have not implemented instance method msg1() in subclass but abstract method msg2() has been implemented (overridden) in the subclass.
Let’s make a program where an abstract class can have a data member, constructor, abstract, final, static, and instance method (non-abstract method).
Program source code 4:
In this example program, after object creation, the constructor of non-abstract subclass will be called immediately. In the first line of constructor, internally super will call the constructor of an abstract class. The control of execution will be immediately transferred to the constructor of abstract class.
Therefore, the first output is “AbstractClass constructor”. After executing abstract class constructor, control of execution again comes to execute subclass constructor. The second output is “AbsTest class constructor”.
Why abstract class has constructor even though you cannot create an object?
We cannot create an object of abstract class but we can create an object of subclass of abstract class. When we create an object of subclass of an abstract class, it calls the constructor of subclass.
This subclass constructor has super in the first line that calls constructor of an abstract class. Thus, the constructors of an abstract class are used from constructor of its subclass.
Here, we will create a program where an abstract class Employee contains instance variables, constructor, and concrete method. A subclass Engineer extends abstract class Employee.
Program source code 5:
Now consider the above program. When we have created an object of subclass, it immediately calls Engineer class constructor. In the first line of constructor, super keyword calls the super class constructor (Employee).
Thus, the super class constructor is executing when creating an object of subclass. By executing this super class constructor, JVM is initializing to non-static variables name and id in the abstract class.
Now suppose if abstract class does not allow to define constructor, in such a case, is it possible to initialize the value of non-static variables in abstract class?
Answer is No, because without object creation of abstract class, we cannot initialize non-static variables. Therefore, an abstract class allows constructors to initialize variables.
Program source code 6:
In the preceding program, we created abstract class reference that refers to its subclass objects. Using superclass reference, we are accessing all methods of subclass in the main() method excepting newly added method in the subclass. But if it is possible to access all the members of subclasses by creating objects of subclass then,
Why should we create reference to superclass (abstract class reference)?
We should create a reference of the superclass to access subclass features because superclass reference allows only to access those features of subclass which have already declared in superclass. If you create an individual method in subclass, the superclass reference cannot access that method.
Thus, any programmer cannot add their own additional features in subclasses other than whatever is given in superclass.
Advantages of Abstract class in Java
The main advantages of using abstract class are as follows:
1. Abstract class makes programming better and more flexible by giving the scope of implementing abstract methods.
2. Programmer can implement abstract method to perform different tasks depending on the need.
3. We can easily manage code.
Hope that this tutorial has covered almost all important points related to abstraction and abstract class in java with real-time example programs. I hope that you will have understood and enjoyed this topic.
Thanks for reading!