Open In App

Are All Methods in a Java Interface are Abstract?

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.

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:



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




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 :

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




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:

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




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:

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




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:

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




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();
    }
}




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 :

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




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

Article Tags :