Java Abstraction
A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).
Before learning the Java abstract class, let’s understand the abstraction in Java first.
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
The abstract keyword is a non-access modifier, used for classes and methods:
- Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
- Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
Abstraction lets you focus on what the object does instead of how it does it.
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}There are two ways to achieve abstraction in java
- Abstract class (0 to 100%)
- Interface (100%)
Points to Remember
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
Abstract class having constructor, data member and methods
An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}Why can’t we create an object of an Abstract Class?
You might be wondering why can’t we instantiate an abstract class or create an object from it? Suppose, if Java allows you to create an object of the abstract class and use this object.
Get Mehmet Başak’s stories in your inbox
Join Medium for free to get updates from this writer.
Now, if someone calls the abstract method through the object of the abstract class then what would happen? There would not be any actual implementation of the called method.
Also, an abstract class is like a guideline or a template that has to be extended by its child classes for the implementation.
Code to illustrate that object creation of an abstract class is invalid:
As discussed above, we cannot instantiate an abstract class. This program throws a compilation error.
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type ParentClass
References:
w3school.com
techvidvan.com
javatpoin.com








