How to Define Abstract Class in Java?
What is an 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 define abstract class?
In java, we can define an abstract class using the keyword “abstract”. As we all know an abstract class cannot be instantiated directly means it need to extended by other classes.
Here are some key points to define an abstract class in java:
- A class that is abstract is declared using the abstract keyword.
- The abstract keyword is used to declare abstract methods, which are then followed by a semicolon. These abstract methods must have an implementation provided by subclasses.
- An abstract class may also define concrete methods. These methods already have a full implementation, thus subclasses are not necessary to override them.
- Instance variables and constructors are both possible in abstract classes.
- Subclasses can access the constructors and variables of the abstract class thanks to the protected access modifier.
For example:
//here how we define an abstract class in java
public abstract class AbstractClass { // Variables, constructors, and methods can be defined here // Abstract method declaration (no implementation) public abstract void abstractMethod(); // Concrete method with implementation public void concreteMethod() { // Method implementation } // You can also have variables and constructors in an abstract class // These can be accessed by the subclasses // Abstract classes can have instance variables protected int myVariable; // Abstract classes can have constructors public AbstractClass() { // Constructor implementation } }
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.
- Limited inheritance: Java only supports limited inheritance, so a subclass can only extend one abstract class because multiple inheritance is not supported. A subclass cannot inherit from another class if it already extends an abstract class. The adaptability of class hierarchies is constrained by this restriction.