-5

This is pretty much the same as the question asked here.

But my problem extends this.

I can change it to "A extends B" but then I have to make my methods non-static and that just screws my whole system over.

Is there any way I can fix this?

Okay let's say I've got a class A with method AA and method BB

public class  A {

    public static final BB (){

    }
     public static final AA (){

    }
  }
}

That's set now I've got another class that Implements it but it also has it's own version of method AA and BB.

public class AImpl implements A {

    public BB (){

    }
     public AA (){

    }
  }
}

now if I try to extend the class I will get the error the "instance method cannot override the static method from A".

So then it says I can fix this by removing the static from class A.

I do that then it tells me that nearly all of the other maybe 50 classes can't reference the non-static method from class A's method BB.

Community
  • 1
  • 1
no1dead
  • 9
  • 1
  • 1
  • 2
  • 1
    Can you please post what you're actually trying to do, preferably with code? – Antimony Apr 21 '13 at 23:16
  • In the example you gave, your first section of "code" is a class. Your second section tries to implement it. Classes can extend classes, but they can only implement interfaces. – jschabs Jul 10 '13 at 19:24

1 Answers1

6

interfaces cannot have static methods. They can have static final constants but not methods.

If you want to have static methods you need to use an abstract class

interface Iface {
    void doStuff();        
}

abstract class Base implements Iface {
    static void doStaticStuff() {

    }
}

EDIT

You cannot override static methods. You also cannot implement other classes.

You extend other classes.

You cannot solve your issue unless you either make the methods in one class non static or make the methods in the other class static

static class A {
    static void a(){}
}

static class B extends A {
    static void a(){}
}

What you are trying to do does not make any sense. How would an instance method override a static method?

Notice this does not override the a() method in A it simply creates another a() method in B. The call A.a() will call the method a() in A and the class B.a() will call the method a() in B.

It doesn't really make sense to have static methods called the same thing in two different classes as it causes problems with static imports and gives the impression that the method in one class overrides the other. Which it doesn't.

In any case, even if you were able to override static methods; yours also also final which makes that impossible. Even if you remove the static you will not be able to override the methods until you also remove the final.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166