+5

Default method access of an interface

Why is it that a default method declared in an interface when implemented by a class cannot be overridden when we use any other access modifier except public. Why can't it be accessed by default since both will have the same access.

11/16/2019 6:53:02 AM

Avinesh

17 Answers

New Answer

+6

Avinesh Interfaces define the public contract for interacting with an object from an external object without knowing the concrete class type. Only the interface type is required to be known in such cases. Therefore, interfaces must always be implemented with a public accessor. It wouldn't make sense to define a private or protected interface for members that are not visible to external code.

+4

In Java you cannot reduce the visibility of a method from more visible to less visible in inheritence. All methods of interface are public by default. Overriding it with different access modifier other than public will be restricting methods freedom. This is applicable throughout the inheritance chain in Java then whether you are implementing an interface or extending a class. Note package access (i.e. no access modifier specified) is less visible than protected hence in derived class a method defined with no access modifiee can be overriden with protected or public access modifier

+4

Avinesh interface methods are public by default. "default" keyword can be used with interfaces only. You cannot override an interface method as protected in the implementing class as it will make the method less visibile. I said nearly the same thing earlier too am I missing something? interface Test { default void Show(){ System.out.println("HELLO"); } } public class Main implements Test { public static void main(String[] args) { new Main().Show(); // prints "hello" instead of "HELLO" } @Override public void Show() { System.out.println("hello"); } }

+4

David Carroll Java interface can have private methods and they MUST have implementation. So they seems to be "default" by default. static method too can be private in interface. I suppose their usage is more like helper methods for default or static methods. interface Test { private void show(){ System.out.println("HELLO"); } default void Show() { show(); } } public class Main implements Test { public static void main(String[] args) { new Main().Show(); } }

+3

~ swim ~ Then is static also public inside the interface? interface A{ static void show(){ System.out.println("hello"); } }

+3

Avinesh Yes they are public by default but static methods cannot be overridden. If the implementing class provides a definition of static method of the interface with a different access modifier (even with public modifier)then it hides the interface method implemention. And it's access through classname(or object) will invoke class specific implementation if accessible.

+3

Ananta Khare Don't post unrelated comments in someone's else code. Do you see anything about web development being discussed here?

+2

~ swim ~ I completely know what you just told me. Java 8 has introduced default and static methods and these are different from normal methods in the interfaces since default and static can have implementation within the interface. For eg- interface A{ default void show(){ System.out.println("hello"); } } This interface when implemented by a class should allow us to override the method by using default, protected or public just like you mentioned above. But it doesn't work.

+2

~ swim ~ David Carroll I am really sorry if I am asking something that is not making sense. I have spent close to two days on it already to understand.

+2

Avinesh And why it shouldn't? Either you are are not reading my comments properly or not understanding it properly. I said if the implementing class provides an implementation of static method of the interface with any access modifier then it hides interface method implementation. And if such method is called through classname (or through object) then the class specific implementation is called if it is accessible. main method is still a method of class Program and can access any private method directly (if it is static) or through the object. So it can access private methods Show.

+2

Avinesh Not a problem we are here to reduce your struggle 😁

+2

~ swim ~ Thanks a lot for really making it clear to me. I get it now. David Carroll Thanks for that explanation.

+2

Avinesh You are Welcome friend ☺🙏👍

+2

Abdul Wahab Chattha Don't post random comment(s) if you have nothing to contribute !

+1

~ swim ~ David Carroll First of all I do not understand how a method can be default and public at the same time. Ok for a second let us just keep it aside. If static is also public inside the interface and if it cannot be overridden then I really need an explanation for this code because this thing works. import java.util.*; public class Program implements A { // Any access modifier works private void show(){ System.out.println("hello"); } public static void main(String[] args) { Program obj = new Program(); obj.show(); } } interface A{ static void show(){ System.out.println("hi"); } }

0

👇? 😒 https://www.sololearn.com/discuss/2072420/?ref=app

-2

https://www.sololearn.com/Discuss/2072312/?ref=app