Features 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.
Feature of abstract class in java
There are some main feature of an abstract class in java:
- An abstract class cannot be instantiated: As we all know an abstract class can be base class for the other classes but it cannot be used to produce objects directly.
- Abstract methods may be found in abstract classes: abstract methods are declared without a body, if any class extends the abstract class must implement these methods. In other words, abstract class allows us to define a common interface for its subclasses.
- Non-abstract methods may also be found in abstract classes: these methods contain a body and it doesn’t required any additional implementation before being utilised by the subclasses.
- Instance variables may be present in an abstract class: With the help of abstract class and its subclasses we can make use of these variables.
- A non-abstract class may extend an abstract class: as we all know that every abstract method defined by an abstract class that a concrete class extends must have an implementation provided.
- An abstract class has ability to implement interfaces: abstract class can give a default implementation for any interfaces methods it provides, which subclasses can override.
- A contract can be enforced using an abstract class: With the help of abstract class we can impose a contract on its subclasses by specifying a set of abstracts methods, requiring that they fulfil a certain set of requirements.
In other words, abstract classes are a useful tool for creating hierarchies of related classes in java. It helps us to define a common interface for a group of related classes while allowing for flexibility in the implementation of each individual classes.
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.