0

Let's say I have a class, called BaseClass, that looks something like this

class BaseClass{
    String var1;
    int var2;

    public void methodToBeOverridden(){}
}

and I need to create a significant number of classes that inherit this class. They will all override methodToBeOverridden(), but will all be instantiated in a static context. Since they all need to be static, they cannot be put in a single .class file to my understanding. Is there any cleaner way to do this than creating a separate .class file for each instance? I really doubt there is, but I thought I'd ask here before I got started on it just in case.

3
  • You can create static nested classes in the same class file. You can also declare more than one top-level class in a file if they're not public, but that's generally discouraged. Commented Aug 16, 2016 at 0:22
  • I don't think this BaseClass is necessary. I suggest use enum to implement it. Commented Aug 16, 2016 at 0:31
  • Maybe also a job for a functional interface and method handles. You can have all variants in a single class. Commented Aug 16, 2016 at 0:56

2 Answers 2

2

An approach I take is to override implementations for an enum which implements an interface From https://github.com/OpenHFT/Chronicle-Engine/blob/master/src/main/java/net/openhft/chronicle/engine/map/remote/MapFunction.java

public enum MapFunction implements SerializableBiFunction<MapView, Object, Object> {
    CONTAINS_VALUE {
        @Override
        public Boolean apply(@NotNull MapView map, Object value) {
            Class vClass = map.valueType();
            return map.containsValue(convertTo(vClass, value));
        }
    },
    REMOVE {
        @Override
        public Object apply(@NotNull MapView map, Object o) {
            Class kClass = map.keyType();
            Class vClass = map.valueType();
            KeyValuePair kf = (KeyValuePair) o;
            return map.remove(convertTo(kClass, kf.key), convertTo(vClass, kf.value));
        }
    },
    REPLACE {
        @Override
        public Object apply(@NotNull MapView map, Object o) {
            Class kClass = map.keyType();
            Class vClass = map.valueType();
            if (o instanceof KeyValuePair) {
                KeyValuePair kf = (KeyValuePair) o;
                return map.replace(convertTo(kClass, kf.key), convertTo(vClass, kf.value));
            }
            KeyValuesTuple kf = (KeyValuesTuple) o;
            return map.replace(convertTo(kClass, kf.key), convertTo(vClass, kf.oldValue), convertTo(vClass, kf.value));
        }
    },

This allows me to have many implementations of the same method in a single .java file. Note: this still creates multiple .class files.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not gonna lie, I have no idea what a lot of that was. But you steered me in the direction of enums, which worked flawlessly.
0

I seriously doubt that each of these classes has substantially different code inside methodToBeOverridden. They probably all differ by some piece of data - or maybe they fall into a few families like that - and you should make that piece of data be a constructor argument to the class and not have very few derived classes if any.

3 Comments

You seriously doubt based on what evidence? Or is this mere guesswork?
@EJP based on my experience in Java. Just sounds like a design smell and this is probably why. I'd be interested if the OP posts more but in the meantime I've likely left a pretty substantial hint that hopefully will get them to stop coding into a hole. I also may be wrong, which is fine.
You know what they say about making assumptions. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.