How to Call Concrete Method of Abstract Class in Java?
What is concrete method?
A method that has a full implementation in a class is referred to as a concrete method or a non-abstract method. It has a direct callback interface and offers a particular functionality. Contrary to abstract methods, which only declare a signature without providing implementation information, concrete methods have a defined body that contains executable code.
With the help of java we can allow for the inclusion both abstract and concrete methods in the same class. If we want to provide shared functionality across all subclasses, concrete methods are frequently used in abstract used in absgtract classes. Subclasses don't need to provide their own implementation in order to inherit these concrete methods.
How to call concrete method of abstract class in java
Without first creating an instance of a subclass that extends the abstract class, you cannot call a concrete method of an abstract class directly in Java. However, a non-abstract method can be defined in an abstract class and called from either the abstract class itself or one of its subclasses.
For example:
abstract class AbstractClass { public void abstractMethod() { // This is an abstract method. // It must be implemented by any concrete subclass. } public void concreteMethod() { // This is a concrete method. // It can be called directly or overridden by a subclass. System.out.println("This is a concrete method."); } } class ConcreteClass extends AbstractClass { @Override public void abstractMethod() { // Implement the abstract method from the abstract class. System.out.println("Implemented abstractMethod in ConcreteClass"); } } public class Main { public static void main(String[] args) { ConcreteClass obj = new ConcreteClass(); obj.concreteMethod(); // Call the concrete method of the ConcreteClass. } }
Output:
This is a concrete method.
Explanation:
In this illustration, the AbstractClass defines a concrete method concreteMethod() in addition to an abstract method abstractMethod(). The ConcreteClass provides an implementation for the abstract method and extends the AbstractClass.
The concrete method concreteMethod() is directly called on an instance of ConcreteClass that is created in the main method. The result will be:
Why we use concrete method of abstract class in java
Java's abstract classes contain concrete methods that have multiple uses and offer a variety of advantages. The following justifies the utilisation of concrete methods in abstract classes:
- Giving default behaviour: Concrete methods in an abstract class may give a default implementation for shared features that are present in all subclasses. Subclasses don't have to provide their own implementation; they can simply inherit and use these methods as-is. This encourages code reuse and cuts down on duplicative code.
- Coding structure and uniformity: Abstract classes are frequently used to specify a base class that encapsulates shared characteristics and actions for a collection of related classes. You can organise and centralise common functionality in the abstract class's concrete methods. This encourages the organisation of the code and upholds consistency.
- Supporting template method pattern: The template method pattern allows subclasses to provide specific implementations for particular steps while defining the skeleton of an algorithm in an abstract class. While abstract methods are used to define the specific steps that differ across subclasses, concrete methods in the abstract class can be used to define the algorithm's common steps.
- Forward compatibility: An abstract class's concrete methods can be added to or changed without harming already-existing subclasses. Existing subclasses automatically inherit and use the updated behaviour since concrete methods have a default implementation without requiring any modifications. This makes it possible to retroactively expand the functionality of current classes.
- Client-side simplicity: Client code does not need to create a subclass in order to access concrete methods of an abstract class. This makes it easy for clients to use the predefined functionality and makes interacting with the abstract class and its subclasses less complicated.
In general, concrete methods in abstract classes offer a means of defining default behaviour, sharing common functionality, fostering code organisation, supporting design patterns, ensuring backward compatibility, and providing ease of use for clients. They are essential to the object-oriented programming paradigm and significantly improve maintainability and code reuse.