Saturday, October 25, 2025
HomeLanguagesJavaJava Interface methods

Java Interface methods

There is a rule that every member of interface is only and only public whether you define or not. So when we define the method of the interface in a class implementing the interface, we have to give it public access as child class can’t assign the weaker access to the methods.
As defined, every method present inside interface is always public and abstract whether we are declaring or not. Hence inside interface the following methods declarations are equal.

void methodOne();
public Void methodOne();
abstract Void methodOne();
public abstract Void methodOne();

public : To make this method available for every implementation class.
abstract : Implementation class is responsible to provide implementation.
Also, We can’t use the following modifiers for interface methods.

  • Private
  • protected
  • final
  • static
  • synchronized
  • native
  • strictfp




// A Simple Java program to demonstrate that
// interface methods must be public in 
// implementing class
interface A
{
    void fun();
}
  
class B implements A
    // If we change public to anything else,
    // we get compiler error
    public void fun()
    {
        System.out.println("fun()");
    }
}
  
class C
{
    public static void main(String[] args)
    {
        B b = new B();
        b.fun();
    }
}


Output:

fun()

If we change fun() to anything other than public in class B, we get compiler error “attempting to assign weaker access privileges; was public”

This article is contributed by Twinkle Tyagi. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS