An Abstract Class in Java one that has the “abstract” keyword used to declare it. It can have both concrete methods (ordinary methods with a body) and abstract methods (methods without a body). Abstract methods cannot be included in a regular class (a non-abstract class). We will discover what an abstract class is, why we use it, and the guidelines we need to follow when using it in Java in this guide.
You cannot make an object of an abstract class since it cannot be instantiated. Check our Java online course to learn more.
Why do we need an Abstract Class in Java?
Say we have a class called Animal with the method sound() and subclasses like Dog, Lion, Horse, Cat, etc. Since each animal has a unique sound, it serves no purpose to implement this method in the parent class. This is so that each child class can provide its own implementation details, such as the word “Woof” or “Roar” in this method, which must be overridden by each child class.
Implementing this function in the parent class is therefore pointless since we know that all animal child classes will and should override it. Thus, making this function abstract would be a prudent decision since doing so forces all child classes to implement it (otherwise, a compilation error will occur) and frees up the parent class from having to implement it.
You must declare this Abstract Class in Java because the Animal class contains an abstract method.
Now that each animal must have a sound, we made it necessary for the child class to provide implementation details by making this function abstract. We can guarantee that every animal has a sound by doing this.
Therefore, in situations like these, we typically designate the class as abstract, then subsequently concrete classes extend them and replace the methods as necessary.
Abstract class declaration
Although an abstract class describes the methods, it may not really implement them all.
- Note 1: There are situations in which it is challenging or frequently unnecessary to implement all of the parent class’s functions. When this occurs, we can designate the parent class as abstract, converting it into a unique class that cannot stand alone.
All the methods that are marked as abstract in the parent class must be implemented by any classes that are derived from the abstract class.
- Note 2: You cannot make an object of an abstract class because it cannot be instantiated. To utilise this class, you must first construct another class that extends it and implements abstract methods before you can use an object from that other class to call non-abstract methods of the parent class as well as implemented methods(those that were abstract in parent but implemented in child class).
- Note 3: If a child does not implement all the abstract methods of the abstract parent class, then the child class must need to be declared abstract as well.
Do you know? Since abstract class allows concrete methods as well, it does not provide 100% abstraction. You can say that it provides partial abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
Interfaces on the other hand are used for 100% abstraction (See more about abstraction here).
You may also want to read this: Difference between abstract class and Interface in Java
A child class must also be marked abstract if it does not implement all of the abstract methods of the abstract parent class.
Are you aware? Since abstract classes also permit concrete methods, they do not completely provide abstraction. You may say that it offers some limited abstraction. With abstraction, you “hide” unneeded information from the user and only display “relevant” information.
Conversely, interfaces are used for complete abstraction (learn more about abstraction here).
Possibly read this as well: Java’s distinction between abstract classes and interfaces.
Why can’t we create the object of an abstract class?
Because these classes aren’t complete, they have abstract methods with no bodies; therefore, if Java lets you create objects from these classes, what would happen if someone called the abstract method while utilising those objects? The technique would not actually be implemented to use.
A tangible thing is another reason. Since an abstract class functions as a template, you must build upon it before using it.
An illustration to show that it is not permitted to create objects from abstract classes
A class that is abstract cannot be instantiated. There is a compilation mistake in this program.
Note: If a class extends an abstract class, then it must implement all of its abstract methods; otherwise, the class must be declared abstract.
Abstract class vs. Concrete class
A concrete class is referred to as one that is not abstract. Animal is an abstract class in the example given above, while Cat, Dog, and Lion are concrete classes, as we saw at the beginning of this guide.
- Unless it is extended by another class, an abstract class is useless.
- If an abstract method is declared in a class, the class must also be declared abstract. In a concrete class, there cannot be an abstract method. Contrary to popular belief, a class can still be designated as abstract even if it has no abstract methods.
- It may also use concrete, non-abstract methods.
Let’s look at some fundamentals and an example of an abstract method right now.
- Abstract methods don’t have bodies.
- A semicolon (;) should always follow the declaration.
- It needs to be ignored. It is necessary to extend an abstract class, just as it is necessary to override an abstract method.
- In order to have abstract methods, a class must be declared abstract.
Finally, It should be noted that a class that extends an abstract class must override every abstract method.
Conclusion You can check our Java training to learn more about Abstract Class.