Page 1 of 1

About Question enthuware.ocpjp.v7.2.1179 :

Posted: Mon Oct 29, 2012 2:27 am
by D3x!
new SimpleInterface() { ... }


if SimpleInterface is an interface, how can you instantiate it?

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Mon Oct 29, 2012 7:38 am
by admin
Exactly the way shown above :) It is actually instantiating an anonymous class that implements SimpleInterface.

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Fri Mar 13, 2015 5:33 am
by admin
It is only partial code. The part that is shown correctly instantiates the anonymous class.

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Sat Sep 05, 2015 2:16 pm
by boyonsea
new ComplexInterface(x) {    
//valid code
}

This is marked as a wrong answer
explanation is - You cannot pass parameters when you implement an interface by an anonymous class.

does that mean, you can pass parameters when implementing interfaces by a regular class?
Can interfaces have constructors?

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Sat Sep 05, 2015 8:58 pm
by admin
boyonsea wrote:new ComplexInterface(x) {    
//valid code
}

This is marked as a wrong answer
explanation is - You cannot pass parameters when you implement an interface by an anonymous class.

does that mean, you can pass parameters when implementing interfaces by a regular class?
Yes, of course. A regular class can have any number of constructors with whatever parameters it needs. And it can implement an interface as well.
Can interfaces have constructors?
An interface can never be instantiated. So there is no question of it having a constructor.

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Fri Feb 26, 2016 8:08 am
by rianne
new SimpleInterface() { ... }
Shouldn't this include a ';' after '}' to be completely valid?

Re: About Question enthuware.ocpjp.v7.2.1179 :

Posted: Fri Feb 26, 2016 9:42 am
by admin
It is not a complete code listing so doesn't really matter but yes, all options must be consistent in this respect.
thank you for your feedback!
Paul.