Results 1 to 7 of 7
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Can you create a class which when implemented requires to have a HashMap and methods

    Hello!
    I want to make several classes which extend different objects and add additional functions to simplify them and make their purpose in my projects more narrow and make their instances easier to use. So an example, Image class which extends BufferedImage and the constructor in Image class directly loads the file without having to create it first and then have to use Try Catch and all that additional code. Now, here is where my question comes in. Can I make an class, an abstract class or something which can be IMPLEMENTED into these several classes such as the Image class, and in doing so those several classes will have to have (like unimplemented methods) a HashMap<String key, ChildClass instance_as_value>, child class being the Image class as an example.

    So I would have something like public class Image extends BufferedImage implements Library, and this class, because it implements Library will have a HashMap<String key, Image value> in it or it's parent class.

    Regards
    augustas656

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    First, I am still not certain what you want to do. And you are also mixing up terminology. BufferedImage extends
    Image, not the other way around. And interfaces, not abstract classes, are implemented. I recommend you provide
    a Short, Self Contained, Correct Example to demonstrate what it is you want to do. But more to the point. You can
    try to do what you want and see if it works or helps. That's a great way to learn.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    Actually,

    Can you like Pre-create a HashMap, so you define the type of Key but not Value, so can I create an unusable HashMap that I don't define the Object value, and possibly Object key, but then I can initialise it later?

    Regards
    Augustas656

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    I'm confused, okay, what I'm trying to do is create something that if I implement, and the class that implements this something will have a HashMap<String, ClassThatsImplementing> and possibly more. I have an Image Class THAT I made not java.awt.Image, I'm not importing it and I know it's unethical to name something like that, but anyway, my Image class extends BufferedImage, I plan to make other classes that extend other classes, all these classes I want to have a HashMap of <String, ThisClass> as a library that stores all created classes, now to make this easier I'm thinking implement something to do this?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    It sounds to me like you want to use generics. Here is
    a rather contrived example.

    Java Code:
    class Foo<K,V> {
        private Map<K,V> map = new HashMap<>();
        public V getValue(K key) {
             return map.get(key);
        }
        public void setValue(K key, V value) {
             map.put(key, value);
        }
    }
    
    class Bar extends Foo<String, Integer> {
           // Now instances of Bar have access to a map
          // which supports String keys and Integer values.
    }
    Regards,
    Jim
    Last edited by jim829; 10-14-2014 at 05:55 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    Hmm, I can't make this work like I want it, nvm

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can you create a class which when implemented requires to have a HashMap and meth

    I find that if you have problems explaining what you are trying to achieve then it's possible you don't have a clear picture of what it is you are trying to achieve.
    And without a clear picture it's going to be difficult to define the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 08-11-2014, 02:11 PM
  2. Replies: 2
    Last Post: 09-16-2013, 02:53 AM
  3. How should an Animation class be implemented?
    By kiregad in forum New To Java
    Replies: 9
    Last Post: 06-18-2010, 07:39 AM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Static Builder and Create Methods for simple class
    By Pilot Ace in forum New To Java
    Replies: 3
    Last Post: 08-30-2009, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •