I am trying to learn how to create an interface
. I have read different articles and seem to be missing some basic concept.
I want to create a class that when it is done doing something, it will notify any listening classes that it has finished. For example, I want to instantiate a version of MySingletonClass
in my MainActivity
. Have MySingletonClass
do something from MainActivity
and then when finished, call someMethod
that has been Overridden in MainActivity
to
But when I create my interface I get the error:
Class is public, should be declared in a file named filename.java
Here is the error on screen:
Here is my code:
package com.mycompany.myapplication;
public interface MySingletonInterface {
void someMethod();
}
public class MySingletonClass {
private static MySingletonClass ourInstance = new MySingletonClass();
private MySingletonInterface msi;
public static MySingletonClass getInstance() {
return ourInstance;
}
private MySingletonClass() {
}
public void doSomething(){
msi.someMethod();
}
}
Is this the way I should be doing it? Is there some better best-practice? Again I am learning java/android.
I come from an iOS background and in iOS we have what is called a delegate pattern, it's a way for a class to notify another class that it has finished doing something. Is there something like that in Java?