About Question enthuware.ocpjp.v7.2.1179 :

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
D3x!

About Question enthuware.ocpjp.v7.2.1179 :

Post by D3x! »

new SimpleInterface() { ... }


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

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

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

Post by admin »

Exactly the way shown above :) It is actually instantiating an anonymous class that implements SimpleInterface.

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

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

Post by admin »

It is only partial code. The part that is shown correctly instantiates the anonymous class.

boyonsea
Posts: 16
Joined: Fri Sep 04, 2015 5:06 pm
Contact:

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

Post 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?

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

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

Post 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.

rianne
Posts: 5
Joined: Fri Jan 15, 2016 8:49 am
Contact:

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

Post by rianne »

new SimpleInterface() { ... }
Shouldn't this include a ';' after '}' to be completely valid?

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

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests