Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement an interface from a different file?

I am new to Java and I was wondering how I would go about doing something like this.

Interface file:

public interface ExampleInterface {
    void doSomething();
}

Implementation file (in same directory):

public class ExampleImplementation implements ExampleInterface {
    void doSomething() {
        // code goes here
    }
}

EDIT: Hehe, nobody noticed that I forgot to add the class keyword.

like image 322
Tyler Crompton Avatar asked Feb 24 '23 13:02

Tyler Crompton


1 Answers

Everything looks good in your example.

Remeber that java has packages and in above example you don't precise package so interface and class are in default package. You must put this files in some directory and then compile class file.

To compile use:

javac ExampleImplementation.java

Or better, use some IDE (Eclipse or Netbeans) then if something will be wrong IDE will be notify you and tell what error you encountered.

like image 123
lukastymo Avatar answered Feb 27 '23 03:02

lukastymo