Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1478 :

Posted: Tue Jan 19, 2016 4:51 pm
by ntotomanov
Hello,

This question discusses the usage of super with interfaces.
super.methodName(...) is a valid way to invoke a super class's method from anywhere within a subclass's method. But it works only for classes. You cannot invoke the interface's default method using this technique. In fact, if a class (or an interface) overrides a default method of an interface, there is no way to invoke that default method from that class (or interface).
I just wanted to add some additional information in this concern:

In Java 8 has been added the following:
X.super.m(...)
where X is an interface whose method m you want to call from some class.
So if your class TestClass implements interface B and you want to use the default method hello from this interface:

Code: Select all

class TestClass implements A {
	public static void main(String... args) {
		new TestClass().hello(); // Will print "In interface B:hello"
	}

	public void hello() {
		A.super.hello(); 
	}
}

interface A {
	default void hello() {
		System.out.println("In interface B:hello");
	}
} 

Also it is possible from an interface to invoke default method implemented in a super interface:

Code: Select all

interface A {
	default void hello() {
		System.out.println("In interface B:hello");
	}
}

interface B extends A {

	default void hello() {
		A.super.hello();
	}
}
class TestClass implements B,A {
	public static void main(String... args) {
		new TestClass().hello(); // Will print "In interface B:hello"
	}

	public void hello() {
		B.super.hello(); 
//in this case we cannot call A.super.hello because the most specific direct super type  is required to be used! 
	}
}

Re: About Question enthuware.ocajp.i.v8.2.1478 :

Posted: Tue Jan 19, 2016 11:23 pm
by admin
You are right. This has now been fixed.

thank you for your feedback!
Paul.