Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Abstract Method in Java

Since Java is an Object-Oriented Language, it supports the concept of Abstraction. Abstraction is defined as the process or technique that helps a Java developer in hiding the implementation details from the users. There are many ways to achieve abstraction in Java. Out of those many ways, one way of achieving abstraction is by using the Abstract Method in Java. An abstract method can be defined in an abstract Class or an interface. In this article, we will explore the abstract method in java, its syntax, and how they are implemented with the help of some examples.

Abstract Method in Java

An abstract method is a method that does not have a body. It is a method that is declared in a class but does not provide any implementation. Instead, the implementation of the method is provided by the subclasses of the class in which the abstract method is declared. Abstract methods are used to define a common interface for a group of subclasses. The subclasses provide their own implementation of the abstract method. An Abstract Method in Java can be defined in an Abstract Class or in an Interface.

Syntax of Abstract Method in Java

For defining an Abstract Method in Java, we make use of the “abstract” keyword in Java with the method declaration. The correct syntax for defining an Abstract Method is given as.

abstract returnType methodName(parameterList);

Here, in the above statement

  • abstract: This is the keyword that declares the method as abstract. This also specifies that the method does not contain any implementation.
  • returnType: This specifies the data type of the value that will be returned by the datatype.
  • methodName: It is the name of the method.
  • parameterList: This denotes the list of the parameters that the method accepts.

Rules for Declaring an Abstract Method in Java

Here are the rules for defining abstract methods in Java:

  • Abstract method in Java must be declared using the abstract keyword.
  • Abstract methods must not have a body. They should end with a semicolon instead of a curly brace.
  • Abstract methods must be declared in an abstract class or interface.
  • Abstract methods can only be declared in classes or interfaces that are themselves declared as abstract.
  • All classes that extend an abstract class that has an abstract method must implement the abstract method.
  • Abstract methods can have parameters and a return type, just like any other method.

Examples of Abstract Method in Java

Here are some examples of abstract methods in Java:

Example 1 of Abstract Method in Java
This example shows how to define an abstract method in an abstract class in Java.

Code:

abstract class Animal {
   public abstract void makeSound();
}

class Dog extends Animal {
   public void makeSound() {
      System.out.println("Woof!");
   }
}

class Cat extends Animal {
   public void makeSound() {
      System.out.println("Meow!");
   }
}

public class Main {
   public static void main(String[] args) {
      Animal myDog = new Dog();
      Animal myCat = new Cat();
      myDog.makeSound();
      myCat.makeSound();
   }
}

Output:

Woof!
Meow!

Explanation:
In this example, we have an abstract class Animal that defines an abstract method makeSound(). The subclasses Dog and Cat provide their own implementation of the makeSound() method. When we create objects of the subclasses and call the makeSound() method, it prints out "Woof!" and "Meow!" respectively.

Example 2 of Abstract Method in Java
This example also illustrates the usage of the abstract method in an abstract class in Java.

Code:

abstract class Shape {
   protected double area;

   public abstract void calculateArea();
   public void displayArea() {
      System.out.println("The area is: " + area);
   }
}

class Circle extends Shape {
   private double radius;

   public Circle(double radius) {
      this.radius = radius;
   }

   public void calculateArea() {
      area = Math.PI * radius * radius;
   }
}

class Rectangle extends Shape {
   private double width;
   private double height;

   public Rectangle(double width, double height) {
      this.width = width;
      this.height = height;
   }

   public void calculateArea() {
      area = width * height;
   }
}

public class Main {
   public static void main(String[] args) {
      Circle myCircle = new Circle(5);
      Rectangle myRectangle = new Rectangle(10, 20);

      myCircle.calculateArea();
      myCircle.displayArea();

      myRectangle.calculateArea();
      myRectangle.displayArea();
   }
}

Output:

The area is: 78.53981633974483
The area is: 200.0

Explanation:
In this example, we have an abstract class Shape that defines an abstract method calculateArea() and a non-abstract method displayArea(). The subclasses Circle and Rectangle provide their own implementation of the calculateArea() method. When we create objects of the subclasses and call the calculateArea() method, it calculates the area of the shape and sets it to the area variable in the superclass. When we call the displayArea() method, it prints out the area of the shape. In the main method, we have instantiated the objects of both classes and called the functions calculateArea() and displayArea(), and we get the expected output on the screen.

Example 3 of Abstract Method in Java
As mentioned above, In Java, interfaces can also contain abstract methods. An interface is similar to an abstract class in that it provides a set of methods that a class must implement. The difference between Abstract Class and Interface is that an interface can only contain abstract methods which is not in the case of Abstract Classes.

Here’s an example of an interface containing an abstract method in Java:

Code:

interface Drawable {
   public void draw();
}

class Circle implements Drawable {
   private int x, y, radius;

   public Circle(int x, int y, int radius) {
      this.x = x;
      this.y = y;
      this.radius = radius;
   }

   public void draw() {
      System.out.println("Drawing a circle at (" + x + "," + y + ") with radius " + radius);
   }
}

public class Main {
   public static void main(String[] args) {
      Circle myCircle = new Circle(0, 0, 5);
      myCircle.draw();
   }
}

Output:

Drawing a circle at (0,0) with radius 5

Explanation:
In this example, the interface called Drawable has only one abstract method called draw(). Any class that implements this interface must provide an implementation of the draw() method. the Circle class implements the Drawable interface and provides an implementation of the draw() method. The draw() method prints a message to the console indicating that it is drawing a circle at a particular location with a specific radius. In the main method, we instantiated the object of class Circle by passing the arguments as 0, 0, and 5. This results in the printing of the line “Drawing a circle at (0.0) with radius 5” on the output screen.

Advantages of Using Abstract Method in Java

Using abstract methods in Java has several advantages. Some of them are:

  • Abstraction: Using the abstract method in Java helps in achieving abstraction by hiding the implementation details.
  • Flexibility: Abstract methods provide flexibility to the subclasses to provide their own implementation of the method
  • Code Reusability: Abstract methods allow you to reuse the code in the superclass by providing a common interface for a group of subclasses.
  • Encapsulation: Abstract methods help to encapsulate the behavior of an object by defining a common interface for a group of subclasses.
  • Polymorphism: Abstract methods support polymorphism, which allows you to write code that works with objects of different subclasses.

Limitations of Abstract Methods

Using abstract methods in Java also has some limitations. Some of them are:

  • Limited Functionality: Abstract methods only provide a method signature without any implementation. This limits the functionality of the method and makes it less powerful than a regular method.
  • Complexity: Abstract methods can make the code more complex because they require the creation of subclasses to provide an implementation for the method.
  • Inefficiency: Abstract methods can be less efficient than regular methods because they require the use of dynamic binding to call the method at runtime.

Conclusion
Abstract methods are an important feature of Java that allows you to define a common interface for a group of subclasses. They provide flexibility, code reusability, encapsulation, and polymorphism. However, they also have some limitations, such as limited functionality, complexity, and inefficiency. Overall, abstract methods are a powerful tool that can help you to write better object-oriented code in Java.

Frequently Asked Questions (FAQs)

Some Frequently Asked Questions related to Abstract Method in Java are given below.

Ques 1. Can an interface have non-abstract methods in Java?
Ans. No, all methods in an interface are abstract by default, and they cannot have a method body. Therefore, an interface can only have abstract methods.

Ques 2. Can an abstract method be private or final in Java?
Ans. No, an abstract method cannot be private or final in Java. Abstract methods are meant to be implemented in a subclass, and a private or final method cannot be accessed or overridden by a subclass.

Ques 3. Can an abstract class have non-abstract methods in Java?
Ans. Yes, an abstract class can have both abstract and non-abstract methods in Java. Non-abstract methods are fully implemented methods that can be called directly by the subclass.

Ques 4. Can an abstract method be static in Java?
Ans. No, an abstract method cannot be static in Java.

Ques 5. Can an interface extend an abstract class in Java?
Ans. No, an interface cannot extend an abstract class in Java. An interface can only extend other interfaces.

Leave a Reply

Your email address will not be published. Required fields are marked *