• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class2 extends Class1<T, T>  RSS feed

 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a Java beginner and I have to use a HashMap<Integer, Boolean> but for clarity and simplicity I want to use MyHashMap that accepts by definition <Integer, Boolean>.
Is it possible to extend HashMap for this purpose?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can:
you should probably keep it package private, and also, don't use it in the exported interface of your classes. That is, if you write a method that returns or accepts a map, let it return/accept Map<Integer, Boolean>, and not MyHashMap.
 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I'm a beginner and I'm still at the learning stage. Would you please explain in 5-6 words the things I can't wrap my head around and don't waste your time where I guessed it right:
1. Can I replace "?" with any letter? (Guess: I can)
2. Why extends and not directly Integer and Boolean? (No guess)
3. Can I use a CONSTANT for the Integer? (Guess I can)

Why you advise not to pass MyHashMap but Map? (Guess: for clarity or for not losing options). But I want to restrict the type of argument and I want to show the argument type name to be explicit.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei Marian wrote:1. Can I replace "?" with any letter? (Guess: I can)

No. ? is a special character that denotes wildcards. Look up wildcards at the Java generics tutorials. It's a difficult subject, so it might be hard to understand if you're not too familiar with generics yet.

2. Why extends and not directly Integer and Boolean? (No guess)

I'm guessing you're referring to the copy constructor. For Integer and Boolean it doesn't really matter that much, because they're final, but if Oracle changed that some day, your constructor would also accept stuff like TreeSet<SubInteger, MyBoolean>.

3. Can I use a CONSTANT for the Integer? (Guess I can)

I don't understand the question. If you want, you can do something like: Map<Boolean, Integer> map = new MyHashMap(10, 0.5f);

Why you advise not to pass MyHashMap but Map? (Guess: for clarity or for not losing options). But I want to restrict the type of argument and I want to show the argument type name to be explicit.

Because using an interface gives clients of your code more flexibility than if you use very specific implementations. Why would you want to restrict the type of the parameter?
 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the constant: I'd like to be able to pass GL_DEPTH_TEST and false to a capabilitiesMap<Integer, Boolean> and use this map as an argument to a method setCapability(capabilityMap cm) and use a little encapsulation in the idea that capabilityMap is unknown on the inside. Plus, I wouldn't want setCapability() to accept any Map<Integer, Boolean>.

Thank you again for taking the time.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a capabilitiesMap<Integer, Boolean>? Is it your own type?

The whole point of using Map<Integer, Boolean> is that the user doesn't know (or care) what the inside of the maps look like. Using a more specific type defeats this purpose.

And what is the reason of not wanting setCapability() to accept any type of map?
 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, capabilitiesMap<Integer, Boolean> is my type. I initially made a class Capabilities that had an Integer and a Boolean and used a List<Capabilities> but then I thought maps are more fit for the job. I have this urge to "hide" as much as possible
setCapability() is also mine and enables and disables glCapabilities (z-testing, writing to stencil buffer etc.) and you input in my case, a map with Integers as keys (in fact they are CONSTANTS: GL_DEPTH_TEST, GL_BLEND, GL_LIGHT etc.) and their corresponding state (on/off)
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why not use a Set instead?

 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner. Why did I think I need 2 "sets" when here you presented me with such a concise code? That's must be the answer.
Thank you again for helping me.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Practice makes perfect. Good luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!