File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Confusion about interfaces, creating your own classes, casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion about interfaces, creating your own classes, casting" Watch "Confusion about interfaces, creating your own classes, casting" New topic

Confusion about interfaces, creating your own classes, casting

erin oneill

Joined: Jun 25, 2001
Posts: 4
I have a class that creates 2 HashMaps (for configuration):
1) static (only one occurance)
2) instances of a HashMap (a file of key/value info is read into these).
I use this method to create my HashMaps:
Map cache = new HashMap();
Map conf = new HashMap();
Now I'm going to need some methods that will grab the value for certain keys but first for debugging purposes I want a method that will dump out the contents of either the cache or one of the conf HashMaps (I'll have multiple instances of these). Because the cache is public static it's easy to dump the contents (it works nicely), but I'm having trouble dumping out the contents of the named conf file.
Because I'm using Map (vs. HashMap) to create my conf HashMap do I need to write methods that extend the Map methods of put, get, containsKey, containsValue, etc?? (and then change my class to be:
class Config extends Map {
instead of
class Config {
Currently I just have the constructor, a method to read a file into the HashMap (works nicely), and a method to dump the cache that works and an overload dump with the myConf name & obj.
I created a calling program (with a main) that creates my own instance of Config, tries to dump that instance (it fails), then dumps the contents of the cache.
Here's the relevant code (it throws a castException at runtime).

// cut & paste does not work well in NT (I'm a UNIX person). Any typos are mine. The code does compile....

Suggestions??? HELP??
(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited June 27, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Your method takes a String and a Map.
Your call uses a Map and a String - that seems to be a problem.
class config does not extend Map, so how can you cast a config into one?
Just HAVING a Map as an attribute is not the same as BEING a Map.

"JavaRanch, where the deer and the Certified play" - David O'Meara
erin oneill

Joined: Jun 25, 2001
Posts: 4
Oh I knew that re-typing it all in would cause me to dyslexify something! In my code I send it (Map, String) and in my method I grab it (Map, String). whew. That would have been an easy fix.
I guess my question is this:
I create an instance of a Map.HashMap that I want to Iterate thru (plus us the other HashMap/Map methods on). How do I go about it???
1) have my class extend Map (why should I have too??)
2) Caste (but I've read this is not a good thing to do for performance so I really shouldn't)
3) or ??? what am I missing??
erin oneill

Joined: Jun 25, 2001
Posts: 4
ACK! I got it to work! I just needed to step back from it.
Here's what I did (like usual it was a small simplification)
The method only takes a string
dump(String name)
The calling class calls it like this:
String cName = "eon";
Then the dump method uses the public but not static HashMap var of myConf. I did test the ablity to create more than on instance of myConf & to see if they both got into the cache (they did!).
Now I have another question (I know! I know! but I'm a beginner!)
How come I can't call the void dump() like this:
but must call it with the instances' name like this:
(they both dump the cache)
I think this is rudimentary for Java so I really want to understand this.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I know I'm quite late to post here, but :

You can't call just

Because that would be calling the method dump of MyMain class. Which you don't have. You want the Config.dump();

If you wanted to be able to call dump from a class reference, it would need to be static, otherwise, you must always call it from an instance of a class in which the method is defined.

It would be like calling Without System.out. how can it tell where the method println(String s) is located?
I agree. Here's the link:
subject: Confusion about interfaces, creating your own classes, casting
It's not a secret anymore!