Open In App

Are All Methods in a Java Interface are Abstract?

Last Updated : 16 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, the interface is called a blueprint of a class and it is used to achieve abstraction in java. By using interfaces only we can achieve multiple inheritances in java.

  • Let’s understand the concept of blueprint through an example like a blueprint of the building will consist of properties and behaviors of the building but for every building, it might be different, similar to these interfaces has abstract methods which when implemented by classes will have their own unique behaviors.
  • When implementing an interface we force the class to implement its methods and if it is not implementing it then we need to declare that class abstract. We can have default and static methods in the interface.
  • Java 8 introduced the concept of default methods, the reason they were added in Java 8 is for a certain scenario where the interface was being not comfortable to use. For example, if we have one method that is implemented by many classes and the functionality is the same for each case then for the same functionality we need to write this function again and again in every class so to remove this difficulty in Java 8 default methods were given.
  • By default, all methods are public and abstract until we do not declare it as default and properties are static and final.

All methods in a Java Interface are Abstract!

Abstract methods do not have the body they only have declaration but no definition. The definition is defined by implementing classes. So we look at all the examples where a method can exist with its behavior (body) inside the interface. Now we will try to give body to methods in different scenarios inside interfaces.

Example 1:

  • In this example, we will have one abstract method heightDisplay() (does not have a body) which will be implemented by the class GFG.
  • So GFG class needs to implement heightDisplay() method and to call this method we will create the object of GFG class.
  • So our first example tells us we can have abstract methods in interfaces, and we are doing this to just prove that interfaces contain abstract methods only.

Below is the example of having one abstract method only inside the interface:

Java




import java.io.*;
interface Building {
    // abstract methods
    void heightDisplay();
}
 
class GFG implements Building {
    // implementing the abstract method
    // of building interface
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.heightDisplay();
    }
}


Output

height is 5

Example 2 :

  • In this example, we will have one instance method widthDisplay() which will have its body also. Inside the body, we will be printing one statement.
  • Now as we know interfaces consist only of abstract methods only so when we run this we will get the errors. There will be 3 errors but the first one will be at line 6 error name: interface abstract methods cannot have a body.
  • So we proved by this example we can not have instance methods inside the interface. In simple terms, Instance methods are the methods that are not static. Instance methods will produce different outputs for different methods as different objects have different instance properties on which instance method works on.
  • So this example also supports the claim that the interface can only consist of abstract methods.

Below is the example of having one instance method and one abstract method inside the interface:

Java




import java.io.*;
interface Building {
    // abstract methods
    void heightDisplay();
 
    // instance method with body
    void widthDisplay()
    {
        System.out.pritnln("width is 1");
    }
}
 
class GFG implements Building {
    // implementing the abstract method
    // of Building interface
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
 
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.heightDisplay();
        gfg.widthDisplay();
    }
}


Output

prog.java:10: error: interface abstract methods cannot have body
    {
    ^
prog.java:15: error: GFG is not abstract and does not override abstract method widthDisplay() in Building
class GFG implements Building {
^
prog.java:27: error: cannot find symbol
        gfg.widthDisplay();
           ^
  symbol:   method widthDisplay()
  location: variable gfg of type GFG
3 errors

Example 3:

  • In this example, we will have the final method widthDisplay() which in its body will print the width. Hence, we are defining one final method with its body.
  • This violates the interface rules of having abstract methods only because it has a method body.
  • One thing to note is that we can not use the final keyword with abstract methods so this also gives us the error illegal combination of keywords. But we did not use abstract keyword than how can we get the error of illegal combination. Because by default all methods are abstract inside the interface.
  • So this example states that we can not have final methods inside the interfaces. Hence, this example also shows that we can have abstract methods only inside the interface.

Below is the example of having one final and one abstract method inside the interface.

Java




import java.io.*;
interface Building {
    // abstract methods
    void heightDisplay();
 
    // final methods
    final void widthDisplay()
    {
        System.out.pritnln("width is 1");
    }
}
 
class GFG implements Building {
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.heightDisplay();
    }
}


Output 

prog.java:9: error: modifier final not allowed here
    final void widthDisplay()
               ^
prog.java:10: error: interface abstract methods cannot have body
    {
    ^
prog.java:15: error: GFG is not abstract and does not override abstract method widthDisplay() in Building
class GFG implements Building {
^
3 error

Example 4:

  • In this example, we will have one abstract method widthDisplay() which will have its body inside the interface. As you know abstract methods can not have the body so it will also give us the error the one of the error will be abstract methods cannot have the body.
  • So in this example, we tried to give body to an abstract method even if knew abstract methods can not have a body just to prove that we can only have abstract methods inside the interface.
  • So this example also supports our claim that interfaces can consist of abstract methods only.

Below is the example of having an abstract method with the body inside the interface:

Java




import java.io.*;
interface Building {
    // abstract method
    void heightDisplay();
 
    // giving body to abstract method
    abstract void widthDisplay()
    {
        System.out.pritnln("width is 1");
    }
}
 
class GFG implements Building {
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.heightDisplay();
    }
}


Output 

prog.java:10: error: interface abstract methods cannot have body
    {
    ^
prog.java:15: error: GFG is not abstract and does not override abstract method widthDisplay() in Building
class GFG implements Building {
^
2 errors

Note: Prior to Java 8 we did not have static methods inside interfaces but now we can have static methods inside interfaces.

Example 5:

  • Until Java 8 we did not have static methods inside the interface but from Java 8 onwards we can have abstract methods inside the interface.
  • But in this example, there will be no error. So we can have methods that can have their own body inside the interface one of them being static methods.
  • So from Java 8 onwards it’s not 100 % correct to say that interface can only have abstract methods.

Below is the example of having a static method inside the interface:

Java




import java.io.*;
interface Building {
    // abstract method
    void heightDisplay();
 
    // static method
    static void widthDisplay()
    {
        System.out.println("width is 1");
    }
}
 
class GFG implements Building {
    // implementing the abstract method
    // of Building interface
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
 
        GFG gfg = new GFG();
        gfg.heightDisplay();
        // accessing the static method
        // by using of interface name
        Building.widthDisplay();
    }
}


Java




import java.io.*;
interface Building {
    // abstract method
    void heightDisplay();
 
    // static method
    static void widthDisplay()
    {
        System.out.println("width is 1");
    }
}
 
class GFG implements Building {
    // implementing the abstract method
    // of Building interface
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
 
        GFG gfg = new GFG();
        gfg.heightDisplay();
        // accessing the static method
        // by using of interface name
        Building.widthDisplay();
    }
}


Example 6 :

  • From Java 8 we can also have default methods in it. Default methods allow the interface to have a method that does not need to be implemented by the class implementing that interface.
  • An example where default methods are most suited, suppose you have one interface method that is used by many classes and the functionality is same for all classes in this case you need to implement in every class even though the body is same for each class. So Java 8 introduced the concept of a default method to overcome this scenario.
  • Hence, we can say we can have static and default methods inside the interface and other than these we will have only abstract methods.

Below is the example of having the default method inside the interface:

Java




import java.io.*;
interface Building {
    // abstract methods
    void heightDisplay();
 
    // default method
    default void widthDisplay()
    {
        System.out.println("width is 1");
    }
}
 
class GFG implements Building {
    // implementing abstract method
    // of Building interface
    public void heightDisplay()
    {
        System.out.println("height is 5");
    }
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.heightDisplay();
 
        // calling default method
        gfg.widthDisplay();
    }
}


Output

height is 5
width is 1


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads