How to Create an Instance of Abstract Class in Java?
What is abstract class?
In java, an abstract class which cannot be instantiated, which means it cannot make objects out of it. It act as a foundation or model for classes that augment in it.
A class that is abstract may have both abstract and non-abstract methods. Abstract methods lack a body because they are stated without an implementation. Any subclass that extends the abstract class must implement these methods. On the other hand, non-abstract methods contain a body and can be called straight from the subclass without requiring any additional 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.
How to create an instance of abstract class
Because abstract classes are designed to be extended by subclasses, you cannot directly create an instance of one in Java. An instance of a subclass that extends the abstract class can be created, though. Here is how to go about it:
Make an abstract class:
abstract class AbstractClass { // Abstract methods or other members }
Make an extension of the abstract class that includes implementations for the abstract methods:
class ConcreteClass extends AbstractClass { // Implement abstract methods and define other members }
You can now create a subclass instance:
AbstractClass instance = new ConcreteClass();
Concrete Class is a concrete implementation of the abstract class Abstract Class in this illustration. You can indirectly create an instance of the abstract class by creating an instance of Concrete Class.
You can call methods defined in the abstract class or overridden methods in the subclass using the instance variable to access inherited members.
Code implementation
abstract class Animal { public abstract void makeSound(); } class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); } } class Cat extends Animal { @Override public void makeSound() { System.out.println("Meow!"); } } 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! } }
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.