Java: Interface with new keyword how is that possible?
JavaInterfaceJava Problem Overview
I was reading some sourcecode from Java libraries, and I am confused here;
This code is from Document.java in jaxb library, and ContentVisitor is an Interface in same package, how can we create an instance of Interface with a new keyword? isn't that illegal?
public final class Document {
.
.
private final ContentVisitor visitor = new ContentVisitor() {
public void onStartDocument() {
throw new IllegalStateException();
}
public void onEndDocument() {
out.endDocument();
}
public void onEndTag() {
out.endTag();
inscopeNamespace.popContext();
activeNamespaces = null;
}
}
Java Solutions
Solution 1 - Java
In the code, you're not creating an instance of the interface. Rather, the code defines an anonymous class that implements the interface, and instantiates that class.
The code is roughly equivalent to:
public final class Document {
private final class AnonymousContentVisitor implements ContentVisitor {
public void onStartDocument() {
throw new IllegalStateException();
}
public void onEndDocument() {
out.endDocument();
}
public void onEndTag() {
out.endTag();
inscopeNamespace.popContext();
activeNamespaces = null;
}
}
private final ContentVisitor visitor = new AnonymousContentVisitor();
}
Solution 2 - Java
It's valid. It's called Anonymous class. See here
>We've already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as: > new class-name ( [ argument-list ] ) { class-body } > or: > new interface-name () { class-body }
Solution 3 - Java
It is called anonymous
type/class which implements that interface. Take a look at tutorial - Local and Anonymous Inner Classes.
Solution 4 - Java
That declaration actually creates a new anonymous class which implements the ContentVisitor
interface and then its instance for that given scope and is perfectly valid.
Solution 5 - Java
There's something called anonymous class in java http://www.java2s.com/Code/Java/Class/Anonymous-class.htm
Solution 6 - Java
Do notice where the braces open - you are declaring an inner object (called anonymous class
) that implements ContentVisitor
and all required methods on the spot!
Solution 7 - Java
It is inline interface implementation.Here the idea is to have the compiler generate an anonymous class that implements the interface. Then for each method defined in the interface you can (optionally) provide a method with a suitable signature that will be used as the implementation of the interface's method.
It is the new Oxygene syntax, added to the language to allow Oxygene programmers to work with these interface-based events in much the same way as Java programmers do.
Solution 8 - Java
You actually have just provided the implementation of this interface in an anonymous way. This is quite common and of course possible. Have a look here for more information.
Solution 9 - Java
Since the question is still actual and Java 8 brought in lambda. I must mention it. Lambda comparing with AIC has a couple of advantages.
- readability / introduce functional programming.
- in some cases performance.
But lambda and AIC have different scope. You can't create an instance of Lambda and obtain a reference to lambda itself.