1

An anonymous class can extend only from one class or interface, so I can't do the next :

interface Enjoyable {
    public void enjoy();
}

interface Exercisable {
    public void exercise();
}

public class Test {
    public static void main(String[] args) {
        new Enjoyable implements Exercisable() {
            public void enjoy() {
                System.out.println(":D");
            }
        }.enjoy();

    }
}

It says that :

Enjoyable.Exercisable cannot be resolved to a type

I'm trying to replicate this behavior and I wrote the next code:

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

public class Test {
    public static void main(String[] args) {
        new Enjoyable.Exercisable() {
            public void enjoy() {
                System.out.println(":D");
            }

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

            }
        }.exercise();

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

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

            }
        }.enjoy();

    }
}

And then I get :

Doing exercise !!! :D

Are there another way to simulate It? And way i hace to implement both metods un the anonymous class ?

Thanks

3 Answers3

3

I want an anonymous class who implements 2 interfaces methods

I assume you mean you want an anonymous class which implements two interfaces. You can't, directly.

You can do

interface EnjoyableAndExercisable extends Enjoyable, Exercisable {
}

and then create an anonymous class that implements that.

EnjoyableAndExercisable o = new EnjoyableAndExercisable() {
    @Override
    public void enjoy() {
        System.out.println(":D");
    }
    @Override
    public void exercise() {
        System.out.println("Doing exercise !!!");

    }
};

Note the @Override which will always validate whether you are actually overriding a method or not.

In your code however, this anonymous class

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

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

    }
}.enjoy();

is only an implementation of Exercisable. You just happen to declare a method with the name enjoy within it.

You cannot assign it to a variable of type Enjoyable

Enjoyable ref = new Enjoyable.Exercisable() {
    public void enjoy() {
        System.out.println(":D");
    }

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

    }
}; // nope, compile time error

You can only invoke that method on the new instance creation expression that declares this anonymous type. You cannot invoke it any other way (since it's declared in an anonymous type).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

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.

André R.
  • 1,627
  • 10
  • 14
  • But why you must implements both method s un the anonymous type? – Alejandro Agapito Bautista Jul 03 '15 at 18:38
  • @ Alejandro Agapito Bautista .. sorry i'm not quite sure how to understand your comment ... i implemented both methods in the examples because i just adopted your code ... of course in the first example implementing 'excercise' is superfluous – André R. Jul 03 '15 at 19:00
0

You can create anonymous inner classes based on a non-final class or an interface with

new Exerciseable(){
...
}

However, Java does not allow

new Object() implements Exercisable{
...
}

or

new Object implements Exercisable(){
...
}

The anonymous inner class produces with the new keyword a new instance of a class whose superclass is Object and implements Exercisable. The JCP site hosts a document (https://jcp.org/aboutJava/communityprocess/maintenance/JLS/innerclasses.pdf), belonging to the JSL about inner classes which says:

As already hinted, if an anonymous class is derived from an interface I, the actual superclass is Object, and the class implements I rather than extending it. (Explicit implements clauses are illegal.) This is the only way an interface name can legally follow the keyword new. In such cases, the argument list must always be null, to match the constructor of the actual superclass, Object.

Your second example constructs a type Exercisable which extends Enjoyable. Creating anonymous classes of that interface is legal again.

mp911de
  • 17,546
  • 2
  • 55
  • 95