What is Abstract Class in Java?
- Abstract classes are like any other normal classes in java. The major difference between abstract class and normal class is to create the abstract class we need to use the ‘ABSTARCT’ keyword. It is a separation of class implementation.
- They used to define the very common features of its subclasses. Such type of class refereed to as Abstract class.
- Most important we cannot create an object of an abstract class.
- Abstract classes can contain abstract as well as non-abstract methods. They cannot contain a body of the abstract method, that can only be provided by the subclass, If the subclass is not providing the implementation of the abstract method then we have to make it ABSTRACT explicitly.
- In other words, if a class contains an abstract method then it has to define itself as ABSTARCT.
- By using abstract classes we can group several java classes together it also optimizes the code and makes code more readable and reduces redundancy. It also provides a template for future classes.
Syntax of Abstract Class in Java
How does Abstract Class Work in Java?
Let us discuss how abstract class works in java.
- An abstract class has an abstract method and non-abstract method i.e. abstract method without body and they can have methods with implementation also.
- An abstract class is used to provide the most common feature that is specific to different classes. Subclasses can provide a different implementation of those abstract methods according to their needs or requirement.
- We cannot create an object of an abstract class using the ‘new ‘ operator, but we can still define its constructor which can only be invoked in the constructor of its subclass. Subclass constructor can access a superclass constructor to initialize its variable which might be using in the subclass for further requirement.
Examples of Abstract Class in Java
The following examples are provided below.
public abstract class Human
public abstract void whatTheylike();
public void doTheysleep()
System.out.println("Yes every human require sleep.");
public class Human1 extends Human
public void whatTheylike()
System.out.println("we like to have icecream.");
public class Human2 extends Human
public void whatTheylike()
System.out.println("we like to have coffee.");
public class TestHuman
public static void main(String args)
Human human1 = new Human1();
Human human2 = new Human2();
In the above example, we have HUMAN as an abstract class that defines the common needs, likes and dislikes of a human. There are different types of humans with different likes and dislikes. So every human can provide a specific implementation of what they like. That will be specific to them only.
The main advantage of abstract class is that we have a specific implementation of methods according to a requirement which also leads to reduce redundancy, increases the readability of code, hides the implementation of methods and hence provides partial abstraction.
We can have one more example to understand when we should use Abstract classes.
- We should use abstract class when we want to share common functionality between different classes with specific implementation.
- In abstract classes fields should be not static and final, we can also have concrete methods define as private, public and protected.
Let say we have one Animal class. There are varieties of animals we have on earth and they all are different from each other in some or major sense. It will contain all the common features of all.
Now, this Animal class cannot have methods that are specific to every animal. So by the concept of Abstract class, we can implement this functionality without redundant code.
All animals have a different type of sound, habits, etc. For example Dog, cat, elephant, and snack they all have a different sound. So for this, we can have a generic method in parent class through which all other subclass or child class can provide their own specific implementation.
In parent class i.e. Animal we have one generic abstract method called their Sound (). So every child class needs to override this method and provide their own specific implementation.
Abstract Class and Interface
Below are the distinctions between Abstract Class and Interface.
- Abstract class and interface both are used to achieve abstraction in java. An abstract class provides partial abstraction whereas interface provides 100% or complete abstraction.
- By default variables in an interface are final. But abstract class contains a non-final variable as well. Similarly abstract class can have a static, non–static variable as well. But Interface will only contain a final and static variable.
- Member variables of an abstract class can be like private, public, protected, but they are by default public in case of the interface.
- An abstract class can extend another Java class and implement multiple interfaces, but one interface can only extend another interface. An abstract class can provide an implementation of an interface but an interface cannot do so.
- We use implements and extend keyword to implement and extend interface and classes respectively.
- Through method, we can modify or access the non-static and non-final variables of an abstract class.
- Abstract class used to provide partial abstraction. An abstract class cannot be instantiated using the NEW keyword.
- An Abstract method has no body and always ends with a semicolon (;).
- Abstract class contains abstract and non-abstract methods.
- Subclass of an abstract superclass, need to provide an implementation of all abstract method if it does not provide then it has to declare itself as abstract class.
- A subclass can be abstract even if the superclass is concrete.
- A non-abstract class cannot contain abstract methods. Also, the abstract method is non-static.
- Hence we can say that abstract classes contain abstract and concrete methods as well so they cannot provide 100% abstraction. It is a process of hiding the complex logic from the end-user and shows them only the services. A subclass can be abstract even if its superclass is concrete, and it can be used as a data type also.
- An abstract class may have static fields and static methods. You can use these static members with a class reference.
This is a guide to Abstract Class in Java. Here we discuss how it functions, examples of Abstract Class in Java with distinctions between Abstract Class and Interface. You may also look at the following articles to learn more –