Multiple Inheritance for an Anonymous Class

Multiple inheritance for an anonymous class

Anonymous classes must extend or implement something, like any other Java class, even if it's just java.lang.Object.

For example:

Runnable r = new Runnable() {
public void run() { ... }
};

Here, r is an object of an anonymous class which implements Runnable.

An anonymous class can extend another class using the same syntax:

SomeClass x = new SomeClass() {
...
};

What you can't do is implement more than one interface. You need a named class to do that. Neither an anonymous inner class, nor a named class, however, can extend more than one class.

Why is it possible to instantiate multiple traits in Scala, but not a single one?

Technically, you can't instantiate a single trait or multiple mixed traits directly, but the compiler uses some syntactic sugar that allows you to create anonymous classes that extend them. Let's say we have:

trait A
trait B

When you call new A with B, what's really happening is that the compiler is creating an anonymous class that mixes in both A and B. You get:

final class $anon extends A with B
new $anon()

When you call new A {}, the same thing happens. You get an anonymous class that extends A:

final class $anon extends A
new $anon()

The only difference is syntactical. When creating an anonymous class from a single trait, you are required to at least use braces {} to distinguish it from a class. That is, it is easier to discern whether the template can be constructed, or must be wrapped in an anonymous class to be constructed. With multiple traits (or even a class with traits mixed in), the compiler understands it will always need to create an anonymous class first.

To summarize:

class C

new A {} // Anonymous class that extends A, then constructed
new A with B // Anonymous class that extends A with B, then constructed
new C // Constructed instance of class C
new C with A // Anonymous class that extends C with A, then constructed

Why an anonymous class can't implement two separated interfaces but can implement inner interfaces?

your Exercisable's are not Enjoyable :-)
nesting interfaces this way does not mean that the inner interface is
of the type of the outer interface !

you could just as well have written something like

new Object() {
public void enjoy() {
System.out.println(":D");
}

public void exercise() {
System.out.println("Doing exercise !!!");

}
}.enjoy()
// same for .excercise()

so you are not actually simulating an anonymous class that implements two interfaces.

you can see this when you actually try to assign your anonymous instance to a variable of a type of your interfaces

// this WILL NOT COMPILE !
Enjoyable enjoyable=new Enjoyable.Exercisable() {
public void enjoy() {
System.out.println(":D");
}

public void exercise() {
System.out.println("Doing exercise !!!");

}
}.enjoy();

you could of course do something like this :

interface Enjoyable {
public void enjoy();
}

interface Exercisable extends Enjoyable {
public void exercise();
}

and then create anonymous instances using those interfaces
Unfortunately creating an anonymous instance that implements two interfaces like you are trying to do is not possible.

VB.net anonymous class with inheritance

What you want is not possible (at least not the way you describe it).

From what I think I understood ; you should try to mimic what has been donne for Linq ; an interface (like IEnumerable) with all the method you want (or maybe an abstract class bu that prevent you to inherit from something else) + something else (probably a module if you want them as extension method) defining the Select etc. acting on the interface.

From that point you can create classes which implement the interface and use your custom Select etc. on them

Why Java anonymous classes couldn't implement more than one interface?

Like what problems will we have if the Java designers allowed anonymous classes to implement more than one interface?

In Javas type-system there is precisely one static type for each expression. If you had to choose one static type for anonymous_creature you wouldn't be able to make much use of the variable, which is probably why you wrote

IMammal, I4legged anonymous_creature =
^^^^^^^^^^^^^^^^^

which actually changes Javas type-system fundamentally. (Possibly it has been excluded for the same reason as multiple inheritance, namely in order to keep the language simple.)

Besides, there is a trivial workaround, and that is to introduce a auxiliary interface extending both of them:

interface FourLeggedMammal extends IMammal, I4Legged {
}

and then do

... new FourLeggedMammal() { ... }


Related Topics



Leave a reply



Submit