Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java rule for extends and implements

Tags:

java

While coding I created a class that extends ArrayList<> and implements Iterable<>. The code is :

public class Testclass extends ArrayList<Object>, implements Iterable<Object> { }

Netbeans reported an error:

'{' expected

How to prevents this?

like image 970
Rohit Banga Avatar asked Mar 14 '10 05:03

Rohit Banga


2 Answers

From the comments:

error is '{' expected

code is this public class Testclass extends ArrayList<Object>, implements Iterable<Object> { }

Remove that comma ,. It doesn't belong there. It's just a syntax error.

In the future, please read the error message. If you can't interpret it, even not after a little Googling, just ask it here, complete with the entire error message and the code which caused it. This way you will get better suited answers sooner than guessing randomly for the cause ;)

like image 153
BalusC Avatar answered Sep 22 '22 18:09

BalusC


ArrayList does not implement Iterator. Maybe you mean Iterable?

Anyway, it is not a problem to implement an interface that a superclass already implements (it is just redundant).

What error are you getting? It seems to be about something else. If you are using generics, did you make sure the types match on superclass and interface?

Also, it seems to be against the spirit of the Collection and Iterator interfaces to have an object that is both at the same time. At the very least, it is quite confusing.

like image 35
Thilo Avatar answered Sep 25 '22 18:09

Thilo