Implement method for class in a different class

AmiguelS

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?

Konstantin Yovkov

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
        }
    };
    ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implement a class method inherited from two interfaces with different return types

From Dev

how can i implement specific method from two different abstract class?

From Dev

Accessing method of different class in jsp

From Dev

Accessing method of different class in jsp

From Dev

Calling method from a different class

From Dev

How to implement a class constant that is different for each subclass?

From Dev

Where should I implement my class method?

From Dev

Force child class to implement private method

From Dev

Implement interface with method returning class of type parameter

From Dev

Class must be declared as abstract or implement abstract method

From Dev

parse anonymous class does not implement abstract method

From Dev

How to implement the didSelectRow method of the pickerView class?

From Dev

In TypeScript how to force inherited class to implement a method

From Dev

How to implement class with virtual method on the fly

From Dev

Why AbstractList class implement set() method

From Dev

Implement interface with method returning class of type parameter

From Dev

How to implement the Iterable<> method in a generic class

From Dev

How to implement the didSelectRow method of the pickerView class?

From Dev

Force child class to implement private method

From Dev

How to Implement Extension Method in Abstract Class

From Dev

How implement method logout from another class?

From Dev

How to implement class with virtual method on the fly

From Dev

Implement abstract method inside of scalatest class

From Dev

How to force sub class to implement a static method

From Dev

How to implement transfer method for a account class in python

From Dev

Java child class not able to implement parent class's abstract method

From Dev

How to Implement Parent Class Method From Child Class Methods

From Java

Why is binding a class instance method different from binding a class method?

From Dev

Unable to invoke method declare in class implement generic interface method

Related Related

  1. 1

    Implement a class method inherited from two interfaces with different return types

  2. 2

    how can i implement specific method from two different abstract class?

  3. 3

    Accessing method of different class in jsp

  4. 4

    Accessing method of different class in jsp

  5. 5

    Calling method from a different class

  6. 6

    How to implement a class constant that is different for each subclass?

  7. 7

    Where should I implement my class method?

  8. 8

    Force child class to implement private method

  9. 9

    Implement interface with method returning class of type parameter

  10. 10

    Class must be declared as abstract or implement abstract method

  11. 11

    parse anonymous class does not implement abstract method

  12. 12

    How to implement the didSelectRow method of the pickerView class?

  13. 13

    In TypeScript how to force inherited class to implement a method

  14. 14

    How to implement class with virtual method on the fly

  15. 15

    Why AbstractList class implement set() method

  16. 16

    Implement interface with method returning class of type parameter

  17. 17

    How to implement the Iterable<> method in a generic class

  18. 18

    How to implement the didSelectRow method of the pickerView class?

  19. 19

    Force child class to implement private method

  20. 20

    How to Implement Extension Method in Abstract Class

  21. 21

    How implement method logout from another class?

  22. 22

    How to implement class with virtual method on the fly

  23. 23

    Implement abstract method inside of scalatest class

  24. 24

    How to force sub class to implement a static method

  25. 25

    How to implement transfer method for a account class in python

  26. 26

    Java child class not able to implement parent class's abstract method

  27. 27

    How to Implement Parent Class Method From Child Class Methods

  28. 28

    Why is binding a class instance method different from binding a class method?

  29. 29

    Unable to invoke method declare in class implement generic interface method

HotTag

Archive