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
D3x!
Post
by D3x! » Mon Oct 29, 2012 2:27 am
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:
Post
by admin » Mon Oct 29, 2012 7:38 am
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:
Post
by admin » Fri Mar 13, 2015 5:33 am
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:
Post
by boyonsea » Sat Sep 05, 2015 2:16 pm
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:
Post
by admin » Sat Sep 05, 2015 8:58 pm
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:
Post
by rianne » Fri Feb 26, 2016 8:08 am
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:
Post
by admin » Fri Feb 26, 2016 9:42 am
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.
Users browsing this forum: No registered users and 6 guests