0

We have base class as follow:

public class Base {
    protected static string rule = "test_1";

    public static getRule(){
    /* get rule value from origin class*/
    }
}

We have some classes that extend from base class. For example:

public class Derived extends Base {
     static {
          rule = "test_2";
     }
}

Now we wants to get rule variable, but in some conditions:

  • If user call Derived.getRule(), it return test_2,
  • If in derived class rule variable not init, it returned test_1,
  • I don't want to override getRule in all subclasses for answer the question.

What do I do?

Morteza Malvandi
  • 1,656
  • 7
  • 30
  • 73
  • 2
    Static methods and fields are not subject to overriding. If you have an instance method in your base class, you can override it to return different things in your derived classes. But if you actually want to call `Derived.getRule()` directly on the `Derived` class, you can just define a static method `getRule()` in `Derived`. – khelwood Jul 17 '18 at 07:39
  • Define a method called `getRule()` in `Derived` which hides `getRule` in base. But this seems like a generally ill-advised design. – Andy Turner Jul 17 '18 at 07:40
  • You may not use static, they have to be used in very specific cases, think about your model – azro Jul 17 '18 at 07:40
  • @azro Can you take an example? – Morteza Malvandi Jul 17 '18 at 07:41
  • @MortezaMalvandi I don't know what your doing. And you can just make a getRule method in each and like return "test_2", or define the rule variable in each – azro Jul 17 '18 at 07:43
  • 1
    Basically, **don't** imagine that inheritance works for static stuff. You can even create a totally non-related class and create a static getter that gets `rule` from `Base`, and a setter that sets the `rule` from `Base`. In short, the word `extends` means *nothing* in this context, except that `rule` in the inherited class automatically points to `rule` in direct super class, unless there is already one in the current class. – Jai Jul 17 '18 at 08:07
  • Some reading: [Are static methods inherited in Java?](https://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java) – jannis Jul 17 '18 at 11:04

1 Answers1

1

The problem is, that once the Derived class is used (initialized), Base.rule is changed, and everywhere now test_2 is returned, irrespective of the actual class.

So the technique has to be done without static (in that form). There is a categorical, class level value.

public class Base {
    private static final String BASE_RULE = "test_1";

    public String getRule() {
        return BASE_RULE;
    }
}

public class Derived extends Base {
    private static final String DERIVED_RULE = "test_2";

    @Override
    public String getRule() {
        return DERIVED_RULE;
    }
}

Alternatively you can use marker interfaces - which are not mutual-exclusive however, hence not for some getCategory().

public class Base implements Test1Category {
}

public class Derived extends Base implements Test2Category { ... }

if (base instanceof Test2Category) { ... }
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138