The best example of abstraction can be an ATM machine which can be used for cash transfer, withdrawal, inquiring account balance, etc. We utilize ATM machines to achieve different functionalities but when we put the card in the ATM, we have no idea what operations are happening within the ATM machine. That’s exactly what abstraction classes, methods, and interfaces do in Java.
This write-up demonstrates a detailed understanding of data abstraction in the following aspects:
- Abstract Class in Java
- Abstract Method in Java
- Basic Syntax of Abstract Method and Abstract Class in Java
- How to Use Abstract Method, and Abstract Class in Java
Let’s get started!
Abstract Classes in Java
To create an abstract class, we have to use abstract keyword:
}
An abstract class can have normal methods as well as abstract methods:
public void methodName() { //Normal method
//code
}
- A class will be considered as an abstract class, if it has an abstract method and we must have to specify the abstract keyword while class declaration.
- An abstract class can’t be instantiated, this means we can not create the object/instance of an abstract class.
- In order to access the abstract class, we have to extend/inherit it from some other class which means an abstract class will always be used as a Parent class.
- The abstract class can have fields/class attributes and functions/methods just like a regular class.
Abstract Methods in Java
A method without a body is referred as abstract method and to create an abstract method the abstract keyword is used:
Let’s consider the below-given example for the profound understanding of the concepts.
Example
The below-given example creates two classes: an abstract class “Person” that is inherited by a regular class “Employee”.
Person.java
Consider the below-given screenshot for better understanding:
The above example creates an abstract class containing a couple of class attributes, a parameterized constructor, a regular function, and an abstract function.
Employee.java
public class Employee extends Person {
Employee(int age, String name) {
super(age, name);
}
public void concat() {
System.out.println("Age :" + age + " " + "name :" + name);
}
public static void main(String[] args) {
Employee exp = new Employee(22, "John");
exp.concat();
exp.display();
}
}
The above snippet shows the Employee class extends the abstract class Person, and uses the abstract method “concat()”. Furthermore, anything that is declared in the constructor of Employee class is defined in the constructor of Person class. Within the constructor of Employee class (Child), we use the keyword “super” to invoke the constructor of Person class(Parent).
In the main method, we create an object of the Employee class and pass the integer and string value to it. The constructor then initializes age and name using the passed values and lastly, the concat() and display() methods are called.
The output is shown in the below-given snippet:
The output authenticates that the abstract class “Person” is successfully extended and accessed by the “Employee” class.
Conclusion
In Java, abstraction provides security by means of showing the essential details and hiding certain details from the user, and to create an abstract class or method the abstract keyword is used. Abstract class can’t be instantiated and it can have abstract as well as the regular methods. The abstract method will be declared in the abstract class and defined in the regular class. This write-up presents a detailed overview of abstract classes and methods in java, their basic syntax, and how to implement them in Java.