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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ntotomanov
Posts: 6
Joined: Tue Jan 19, 2016 4:30 pm
Contact:

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

Post 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! 
	}
}

admin
Site Admin
Posts: 10401
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You are right. This has now been fixed.

thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests