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.

// 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 GeeksforGeeks 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 GeeksforGeeks 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



My Personal Notes arrow_drop_up



Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 23 vote(s)






User Actions