There is a class A which has an undefined method called OnEvent. I want this method to be define by the class that instantiates a class A Object.
Like this:
public class A{
    int someVar=1;
    float anotherVar=2;
    public void method1(){ 
        ...
        if( event )
            OnEvent();
        ...
    }
    //OnEvent is not defined!!
    OnEvent();
}
And another class, in a different package:
public class B{
    A objA = new A();
    public void method1(){ 
        //I need to do something like
        objA.setOnEvent( this.OnEvent() );
       }
    OnEvent(){
        //Do something
    }
}
I've looked this up and Interfaces and/or lambda expressions are the way to implements this, but I have been unable to do it successfully. Could you please provide some pointers on how to do so?
You can make the A class abstract (and the OnClick() method abstract, too)`.
Then, you can create anonymous instance(s) of A, with directly providing an implementation for OnClick().
For example:
public abstract class A{
    ...
    public abstract void OnEvent();
}
public class B{
    A objA = new A() {
        public void OnClick() {
            //OnClick implementation
        }
    };
    ...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With