I have a class
(Say FOO
) and it has a method with default
visibility,
like below :
void sayHi() {}
Now, If a override this method in extending class I cannot decrease it's visibility. So I can only use default
or public
.
public class MyClassTest extends FOO {
@Override
// Or I can use public void sayHi()
void sayHi() {
System.out.println("Overriden sayHi");
}
}
Now if I write an Interface in Java 8 with a default method like below :
public interface InterfaceX {
// Java-8 defalu method
default String printName() {
System.out.println("Interface1 default metod");
return "Interface1 default metod";
}
}
Now, if I override this method in a class It should compile if a keep overridden method's visibility default
.
public class Java8InterfaceTest implements InterfaceX{
@Override
void printHello() {
System.out.println("Printing..!");
Interface1.super.printName();
}
}
It says
Cannot reduce the visibility of the inherited method from InterfaceX
I already know that every method in interface
in public
by default, but in the above example we are using default
which I think is one of the access modifier
in java
.
I have following questions:
- Is
default
ininterface
different fromdefault
visibility which is provide when no access modifier applied? - If it's different then how it's different?