QuestionsJavaWhy we use Abstract Method in Java?

Why we use Abstract Method in Java?

Abstract method in Java are methods that are declared but not defined in an abstract class. An abstract method does not have a body, and it must be implemented by any concrete subclass that extends the abstract class.

Must read This Before: Why we use abstract class in java?

Here are some reasons why we use abstract methods in Java

1. To define a contract:

Abstract methods are used to define a contract between the base class and its subclasses. By declaring an abstract method in the base class, we are saying that any subclass that extends the base class must implement the method.

2. To enforce implementation:

Abstract methods ensure that a subclass provides an implementation for a particular method. If a subclass does not provide an implementation for an abstract method, then it cannot be instantiated.

3. To provide flexibility:

Abstract methods provide flexibility to the subclass to implement the method as per its own requirements. This allows for variations in functionality and implementation details in subclasses.

4. To achieve abstraction:

Abstract methods help in achieving abstraction, which is an important principle of object-oriented programming. Abstraction helps in reducing complexity by hiding implementation details and exposing only the necessary information to the user.

5. To enable polymorphism:

Abstract methods play a key role in enabling polymorphism in Java. Since an abstract method is declared in a base class and implemented in its subclasses.

We can use a reference variable of the base class type to refer to objects of any of its concrete subclasses, and call the abstract method on them. This allows for greater flexibility and extensibility in our code.

Share with Friends



Please enter your comment!
Please enter your name here

Must Checkout