We would cover the following:
- What is Abstraction?
- Abstract Classes and Methods in Java
- Example of Abstract Class in Java
- Inheriting an Abstract Class
- Instantiating an Inherited Abstract Class
- Abstract Methods in Java
1. What is Abstraction?
Abstraction in Object Oriented Programming is the concept of making the user focus on the relevant aspects of the problem without getting much involved in the low-level details.
For example, we all use the remote control to operate electronic devices. Here we focus on how to use the buttons to operate the device, but we don’t get involved in the electronic principles that guides the communication between the remote control and the device. This low level details is hidden from the user and in fact is not relevant to the user.
In the same way, in OOP, abstraction hides the implementation details from the user while presenting only the relevant features. This is also referred to as Information Hiding.
Abstraction in java is achieved by the use of abstract classes, abstract methods and interfaces.
2. Abstract Classes and Methods
An abstract class is created using the abstract keyword in the class declaration. The following rules applies to abstract classes and methods:
- Abstract class is declared using the abstract keyword
- An abstract class may or may not contain an abstract method (method without a body)
- If a class contains an abstract method, then that class must be declared as an abstract class
- An abstract class cannot be instantiated i.e you cannot create an object from an abstract class
- To use an abstract class, you must create another class that inherits from the abstract class. This subclass would then provide implementation for the abstract methods
- Derived classes from abstract classed provide implementation for the abstract methods
3. Abstraction Example
Here we would go through an example of an abstract class. Let’s create class called Person.
Listing 1.0: Abstract class for Person
Copy this program in Listing 1.0 into your java IDE and run it. Make sure you add the necessary imports. Everything works fine. The only problem is that this class is abstract and so we cannot instantiate. You can try to instantiate it using the code below in listing 1.1 in the main method and check what happens.
Listing 1.1: Creating an instance of an abstract class
If you run to code above that tries to create an instance of the Person class, you will get the error below:
Listing 1.2: You cannot instantiate an abstract class
4. Inheriting an Abstract Class
To use the abstract class, we need to create a subclass that inherits from it. In Listing 1.3, we create a class Student that extends the Person class.
Listing 1.3: Student Class inherits from the Person class
5. Instantiating the Inherited Class
Now we can instantiate the student class.
From the main method we are going a new Student object of type Student.
We would also create a new Student object of type Person
Listing 1.4: Inheriting an abstract class
Try to run the above code and view the output
6. About Abstract Method
A method can also be declared as abstract by using the abstract keyword. Take note of the following:
- If a method is abstract, then the class containing the method must also be abstract. An attempt to declare an abstract method is a class that is not abstract would produce and error (“abstract method can only be declared in an abstract class).
- An abstract method contains only the method declaration, no body is defined
- Abstract method declaration ends with a semicolon(;)