I know that the following code is wrong because the modifier public
is missed in the header of the method m1()
in class B
. But i wonder "Why?.". Why the code causes a compile error if public
is missed.
Thanks in advance.
interface A{
void m1();
}
class B implements A{
void m1(){
System.out.println("m1");
}
}
By default methods have an access of package-private if no access modifier is specified, which means they are public to the package and class only. However interfaces require methods to be implemented by a class using an interface to be public.
See this stack overflow post for what default access modifiers are for classes and interfaces.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.