What is the advantage of abstract class in Java?
What is abstract class?
An abstract class in Java is one that cannot produce objects since it cannot be instantiated. Basically, we can say that it help us as a starting point or a model for classes that add to it.
In abstract class we can found both abstract and non-abstract methods. It is because they are stated without an implementation and abstract methods lack a body so we use both abstract and non-abstract methods. If we want to implementation on these methods then we need any subclass that helps to extend the abstract class. On the other hand, non-abstract methods have a body and can be directly called from the subclass without the need for further implementation.
If we want to create an abstract class in java, then we will use the abstract keyword in the class definition.
For example:
public abstract class Animal{ // abstract method public abstract void makeSound(); // non-abstract method public void sleep() { System.out.println("zzz"); } }
The Animal class in the example above contains one abstract method, makeSound(), and one non-abstract method, sleep(), and is declared to be abstract. The makeSound() method must be implemented by any subclass that extends the Animal class, however the sleep() method can be used directly without any additional implementation.
Code implementation
Program using abstract class how we can implement abstraction in java:
//Abstract class abstract class Animal { // Abstract method public abstract void makeSound(); } //Subclass of the abstract class class Dog extends Animal { public void makeSound() { System.out.println("Woof!"); } } //Another subclass of the abstract class class Cat extends Animal { public void makeSound() { System.out.println("Meow!"); } } //Main class to demonstrate the usage of the abstract class public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.makeSound(); // Output: Woof! Animal cat = new Cat(); cat.makeSound(); // Output: Meow! } }
Explanation:
Basically, in this program Animal is a abstract class that defines an abstract method makeSound (), Dog and Cat are two subclasses of Animal that implement makeSound () method.
And in the main class we create instances of Dog and Cat and call their makeSound()
Methods. Since Dog and Cat are subclasses of Animal we can use an Animal reference to point to either a Dog or a Cat object.
Output:
Woof! Meow!
Advantages of abstract classes
There are some advantages of advantage of abstract class:
- Encourages excellent design principles: By creating a clear interface and allowing for freedom in the implementation of specific subclasses, abstract classes foster good design principles.
- Creates a template for other classes: Abstract classes can act as a model for other classes by offering a set of shared behaviours and methods that its subclasses can inherit.
- Allows for polymorphism: Polymorphism, or the interchangeability of objects of various types, is made possible by abstract classes. This is so that several subclasses of the abstract class can each implement the common interface.
- Allows for partial implementation: Abstract classes are capable of having both abstract and non-abstract methods, allowing them to give their subclasses a partial implementation.
Disadvantages of abstract classes
- There are some disadvantages of advantage of abstract class:
- Can result in tight coupling: Because abstract classes define a common interface that their subclasses must implement, there is a possibility of developing tight coupling between the abstract class and its subclasses. It’s the reason it could be more difficult to change the abstract class or tis subclasses implementation without also changing other system components.
- Reduces the number of subclasses: with the use of abstract classes we can reduce the number of subclasses that can exist in a system because each subclass can only extend one abstract class at a time.
- Can make code harder to understand: because of abstract classes we make code more difficult to understand particularly if there are many abstract methods or abstract classes are written badly.