0

I have declared the following interface in Java:

public interface ITest {
    void doStuff();
}

which is implemented by another few classes who overwrite the doStuff() method. I then use this interface as the type in a function:

public gonnaDoSomeStuff(ITest fun) {
    fun.doStuff();
}

However, Java (and Eclipse) state that the method is undefined for type ITest. What am I doing wrong?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 3
    Are you sure that you are using *this* `ITest` and not some other interface somewhere else in your project (or your dependencies)? Are you sure that you've *compiled* `ITest`. – Joachim Sauer Sep 18 '12 at 17:30
  • Eclipse's compilied artefacts are likely out of sync with the changes in your file. Save all files, clean and rebuild your project. – munyengm Sep 18 '12 at 17:30
  • I've cleaned and this is the only file ITest in my project. It still presents the same error... – sdasdadas Sep 18 '12 at 17:32
  • 1
    What happens when you hold Ctrl and click `ITest` in Eclipse? Does it jump to that `ITest` class? What happens when you ctrl-click `doStuff()`? – Joachim Sauer Sep 18 '12 at 17:34
  • 1
    If you click on the ITest parameter whilst holding down the shift button does eclipse navigate to the ITest interface you expect it to? – munyengm Sep 18 '12 at 17:36
  • 1
    Joachim / munyengm: you were both correct. I must have hit quick fix and Eclipse applied a generic of the name to my class. It wasn't actually referencing the interface. Thank you! – sdasdadas Sep 18 '12 at 17:38
  • @sdasdadas: that's actually a somewhat common problem for beginners (and still I always forget that possibility). You could post this as an answer so future visitors of this question can find it better (and earn some upvotes in the process). – Joachim Sauer Sep 19 '12 at 07:07
  • I will do so only because my local grocery store accepts upvotes. – sdasdadas Sep 21 '12 at 19:22

2 Answers2

0

It turns out that the class containing my gonnaDoSomeStuff method was appended with a generic, which was being referenced instead of the actual interface.

Wrong

public class Dog<ITest> {
    public gonnaDoSomeStuff(ITest fun) {
        // ...
    }
}

Right

public class Dog {
    public gonnaDoSomeStuff(ITest fun) {
        // ...
    }
}
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
-3

You can not access default declared method in a public class because its scope is limited. You should declared it as public for call in public class.

Waji Shah
  • 1
  • 2
  • This is a good point - but because my subclasses were in the same package this actually didn't pose a problem. But I should have declared it protected for my use-case. See: http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – sdasdadas Sep 18 '12 at 17:49
  • 3
    -1 interface methods are always public in Java. Furthermore [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4) states that it is highly discouraged to redundantly specify public modifier in interfaces. – Fabian Barney Sep 18 '12 at 17:54
  • Am I missing something or is every method declared in an interface public? – svz Sep 18 '12 at 17:54
  • Every method is Public. This can change with the addition of static classes. – Mitch Connor Sep 18 '12 at 21:12