-2

Imagine having the classes:

public class classA {
  public static String name() {
    return "classA";
  }
}
public class classB {
  public static String name() {
    return "classB";
  }
}

Since both classes have the same-named static method, I would like to make something like a parent element that they would relate to that would declare that all its child would implement a static method name().

Using interfaces is impossible since I don't want the declaration of the static method to be inside the interface and static methods cannot be overridden. Also, using an abstract class is also not a solution since I can't declare an abstract static method.

Is there a way of doing what I would want to?

2 Answers2

1

You can't enforce parent child relation using static keyword so no there is no way of doing this.

Eshu
  • 499
  • 4
  • 15
0

To be implemented by a child class, name() method should be abstract. Which isn't possible as it is static. Also study about the implications of inheriting static methods : Are static methods inherited in Java?

atinjanki
  • 483
  • 3
  • 13