• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashTable  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Iam facing problem with iterator. I extended HashTable to FrontController class. Iam using the object of the FrontController in another class.

public class FrontController extends HashSet implements Serializable
{
----
-----
}

public ControllerAction getController(FrontController auth)
{
if(auth == null)
{
return loginAction;
}
else{
Iterator i = auth.iterator();
------------
}
}

Iam getting error at the bold line.

Cant we use Iterator at this context...? Pls explain this code and tell me how to iterate the auth object..?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error you are getting? Please paste it?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the text and the title of your question you say you extend HashTable, but the code shows you extend HashSet.

The code should compile without errors. Are you getting a compiler error or an error at runtime (an exception)? Copy and paste the error message.

This question does not seem to have anything to do with SCJP, so I am moving it to the Java in General (Beginner) forum. Please continue there.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To help you with your immediate problem, we need to know more about what is going wrong, as the previous posters have said. However, I have a comment that is more general.

You have a class FrontController, which I guess is some sort of MVC Controller for a user interface. It extends HashSet. This sounds like bad design.

you should extend a class only when it is valid to say that the subclass "is a" superclass. That is, everything that you can do to an instance of the superclass is also valid as a thing to do to the subclass. For instance, a good use of inheritance would be creating a subclass Car of a superclass Vehicle; a Car "is a" Vehicle.

In your case, it seems unlikely that you can really argue that a Controller really "is a" HashSet. Is it really true that everything that you can do to a HashSet is a valid thing to do to a Controller?

In Java, choosing an appropriate superclass is particularly important, because you can only have one (no multiple inheritance allowed). Extending a class purely for implementation convenience, which seems to be what you are doing, is not a good idea.

I think your design would probably be better if the Controller did not extend HashSet, but instead had a field that was a HashSet. Then expose just those methods of HashSet that are appropriate. This leaves you free to extend another, more appropriate class, if future requirements make that necessary.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!