Home > Back-end >  Tricky inheritance issue when dealing with static variables
Tricky inheritance issue when dealing with static variables

Time:03-31

My understanding is that in Java inheriting a parent class with static variables means that the static variables will simply be shared between the parent and child classes, and trying to "override" static variables will only result in hiding them.

I already have defined class A in my project, with functions that use the static variables defined in class A. I would like to create a very similar class--class B--such that it would have the same functions (without having to duplicate the code for the functions by copy-pasting them into the class B file), but have the functions use static variables defined in class B.

If I simply tried "overriding" the static variables after extending class A, the functions (even when called through an instance of class B) would simply still use static variables in class A (instead of class B) due to "hiding". Is there a way to not duplicate the code for the functions and share the logic (that reference the static variables) but somehow still meet the following requirements?

  1. Keep using static variables
  2. Don't add/force the use of getter methods for the static variables because forcing the use of getter methods would mean that future changes to the code would require remembering to have to use getter methods instead of the variables directly. For example, if we implemented it this way and then someone adds a new function (say newFunction()) that uses the static variable directly (instead of using the getter method), class B would now use the static variables for class A (since getter methods were not used) when newFunction() is called for class B.
public class A {
    public static String CONSTANT_1 = "Some information";
    public static String CONSTANT_2 = "More information";
    public static String CONSTANT_3 = "Something Something";
    public static String CONSTANT_4 = "More stuff, more stuff";


    public void doSomething() {
        // do something with CONSTANT_1
    }

    public void doSomething2() {
        // do something with CONSTANT_2
    }

    public void doSomething3() {
        // do something with CONSTANT_3
    }

    // ...
}

public class B extends A {
    public static String CONSTANT_1 = "abc";
    public static String CONSTANT_2 = "def";
    public static String CONSTANT_3 = "ghi";
    public static String CONSTANT_4 = "jkl";


    // don't want to repeat all the logic in the functions in class A, 
    // want to somehow inherit or share code for functions between classes A and B,
    // but have different static variables being used in the functions
}

What I considered so far but am not fully satisfied with yet:

  1. Converting to Kotlin (not sure how well inheritance would work, with static being replaced with companion objects)

  2. Usage of getter methods as described in the post

  3. Using non-static instead of static (trying to see if we can keep the variables static)

CodePudding user response:

If you want to keep the variables static, you could make the methods in class A accept variables as a parameter.
To use the methods without creating objects of the class make the methods static too.

public static void doSomething(String one) {
    // do something with CONSTANT_1
    System.out.println(one);
}

And you can use the same method as follows,

A.doSomething(A.CONSTANT_1);
A.doSomething(B.CONSTANT_1);

CodePudding user response:

I would define the member variables and methods as non-static members of an abstract class. Then expose a singleton instance of the abstract class in A, which you can use like:

A.getInstance().doSomething();

And then in B, you can expose a singleton instance of a subclass of the abstract class.

In Kotlin, this could look a bit cleaner. You could make A and B each be objects that extend the same abstract class. Or if A or B is also a regular class, then instead give them companion objects that extend the abstract class.

  • Related