Java

Abstract Method In Java

A method is said to be abstract if it has no body (no implementation). In other words, if a class contains an abstract method, it should also be declared abstract. A method must always be specified in an abstract class. Before reading this article, review our previous lesson on Abstract classes, which you can find here: Abstract classes in Java.
In Java, an abstract method looks like this:

public abstract int myMethod(int n1, int n2);

Rules of Abstract Method

1. Abstract methods just have the method signature as seen above; they lack a body.
2. A class should be designated abstract if it contains an abstract method; the reverse is not true, therefore an abstract class does not always need to have an abstract method.
3. If a normal class extends an abstract class, either the extended class must implement all of the parent class’s abstract methods or it must be explicitly designated abstract.

Example 1: abstract method in an abstract class

//abstract class
abstract class Sum{
   /* These two are abstract methods, the child class
    * must implement these methods
    */
   public abstract int sumOfTwo(int n1, int n2);
   public abstract int sumOfThree(int n1, int n2, int n3);
	
   //Regular method 
   public void disp(){
	System.out.println("Method of class Sum");
   }
}
//Regular class extends abstract class
class Demo extends Sum{

   /* If I don't provide the implementation of these two methods, the
    * program will throw compilation error.
    */
   public int sumOfTwo(int num1, int num2){
	return num1+num2;
   }
   public int sumOfThree(int num1, int num2, int num3){
	return num1+num2+num3;
   }
   public static void main(String args[]){
	Sum obj = new Demo();
	System.out.println(obj.sumOfTwo(3, 7));
	System.out.println(obj.sumOfThree(4, 3, 19));
	obj.disp();
   }
}

Output:

10
26
Method of class Sum

Example 2: abstract method in interface

An interface’s public abstract methods are all by default. An interface cannot include concrete (regular methods with bodies) methods.

//Interface
interface Multiply{
   //abstract methods
   public abstract int multiplyTwo(int n1, int n2);
   
   /* We need not to mention public and abstract in interface
    * as all the methods in interface are 
    * public and abstract by default so the compiler will
    * treat this as 
    * public abstract multiplyThree(int n1, int n2, int n3);
    */
   int multiplyThree(int n1, int n2, int n3);

   /* Regular (or concrete) methods are not allowed in an interface
    * so if I uncomment this method, you will get compilation error
    * public void disp(){
    *    System.out.println("I will give error if u uncomment me");
    * }
    */
}

class Demo implements Multiply{
   public int multiplyTwo(int num1, int num2){
      return num1*num2;
   }
   public int multiplyThree(int num1, int num2, int num3){
      return num1*num2*num3;
   }
   public static void main(String args[]){
      Multiply obj = new Demo();
      System.out.println(obj.multiplyTwo(3, 7));
      System.out.println(obj.multiplyThree(1, 9, 0));
   }
}

Output:

21

 

Related Articles

Leave a Reply

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

Back to top button