This write-up will explain the below-listed concepts of the abstract classes using some examples:
- What is an Abstract class?
- Why Java Abstract classes?
- Features of Abstract Classes
- Practical implementation
What is an Abstract Class?
A class declared/created using an abstract keyword is called an abstract class. In java, a method declared with an abstract keyword and without a body (method definition) is known as the abstract method. Here is the basic syntax for creating an abstract class:
abstract void exampleMethod();
void regularMethod(){
//method body
}
}
Here, in the above-given snippet, the class is created with the abstract keyword and it has an abstract and a regular method.
Why Java Abstract classes?
Java abstract classes are used to attain data abstraction. The Java abstraction hides the implementation/critical details from the user and shows only the utility or functionality. For example, an ATM machine serves multiple functionalities like cash transfer, cash withdrawal, and so on. We Know how to avail any of its functionality like cash withdrawal, balance inquiry, etc. However, the working of the ATM machine is kept hidden from us. That’s exactly what an abstract class or data abstraction does in Java.
Features of Abstract Classes
Here, we will present some notable features of the Java Abstract class:
- In Java, we can’t create the object of abstract classes.
- It can’t be created using the final keyword.
- We can create the constructor of an abstract class the same way the regular or non-abstract classes.
- It can have class attributes and member functions.
- It can have abstract, regular, final, and static methods.
- It can have regular final methods. However, an abstract method can’t be declared as a final method.
- A Java class that contains an abstract method must be created using the abstract modifier/keyword. A non-abstract class can’t hold an abstract method.
Practical implementation
Let’s consider the below-given example to get a basic understanding of the Java abstract classes:
abstract void atm();
}
class CashWithdrawal extends ATM_Machine {
public void atm() {
System.out.println("cashWithdrawal class implements the atm method");
}
}
class BalanceInquiry extends ATM_Machine {
public void atm() {
System.out.println("BalanceInquiry class implements the atm method");
}
}
public class ATMExample {
public static void main(String[] args) {
cashWithdrawal cash = new cashWithdrawal();
cash.atm();
BalanceInquiry inquiry = new BalanceInquiry();
inquiry.atm();
}
}
- In this example program, we created one abstract class “ATM_Machine” and three regular classes “CashWithdrawal”, “BalanceInquiry”, and “ATMExample”.
- The “ATM_Machine” contains an abstract method named “atm()”.
- The “CashWithdrawal”, and “BalanceInquiry”classes inherited the “ATM_Machine” class.
- The “CashWithdrawal”, and “BalanceInquiry”classes implemented/defined the atm() method of the “ATM_Machine” class.
- We created the objects of the “CashWithdrawal”, “BalanceInquiry”, classes within the main method of the ATMExample class.
- Finally, we invoked the atm() method using the objects of the “CashWithdrawal”, “BalanceInquiry”, classes
Output
This is how an abstract class works in Java.
Conclusion
A class created with the “Abstract” keyword/modifier in Java is called the abstract class. In Java, abstract classes can’t be instantiated. Abstract classes can have constructors the same way as regular or non-abstract classes. Abstract classes can have class attributes, abstract methods, regular methods, final methods, and static methods. This post explained the concept of abstract classes with the help of examples.