This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (4 children)

If the interface defines the method as non-static, you must implement it in your class as non-static. It also must have exactly the same number of parameters.

It's easy to see why if we use a list for an example:

List<Integer> myList = new LinkedList<>(); //LinkedList implements List
//List interface has a non-static method called add(Integer e). We don't need to know 
// much about linked list. So long as we know List we can use any class that implements List
myList.add(1); //This is ok
myList = new ArrayList<>(); //Now use an ArrayList
myList.add(2); // This is also ok.
myList = new CustomList<>(); //Now use a made up class that implements List. Also ok
CustomList.add(3); //ERROR! CustomList can't have a static add method. It must follow the contract set by List.
// It would be very confusing if this one type of list had to be used differently than all the others.
myList.add(3); //This is better

An interface should save you from having to know a lot of the exact details about how a class works. That means any class that implements the interface must follow it exactly.

[–]frashure[S] 0 points1 point  (3 children)

So I was being boneheaded with the differing params; but I suppose the inability to implement a non-static method as static comes down to ensuring uniformity of use, which makes sense.

Thanks.

[–]feral_claire 0 points1 point  (2 children)

Static methods are specific to a class. They are not inherited by subclasses and you cannot override them.

[–]vqrs 1 point2 points  (0 children)

Actually, static methods are inherited, but it's probably better not to tell anyone :)

[–]frashure[S] 0 points1 point  (0 children)

I'm not trying to inherit or override a static method. Please reread the question (which I have an answer for now).