Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we subscribe an abstract method #342

Open
adhamenaya opened this issue Aug 21, 2016 · 3 comments
Open

Can we subscribe an abstract method #342

adhamenaya opened this issue Aug 21, 2016 · 3 comments

Comments

@adhamenaya
Copy link

Can we put the annotation @Subscribe to an abstract method in the parent class only and don't put it to the implementation methods in the child classes?

For example:

abstract class Parent {  
        @Subscribe 
        public abstract void sayHi(EventMessage em); 
}
class ChildA{
        public void sayHi(EventMessage em){
                 System.out.println("Hi I am ChildA");
       }
}

Will the EventBus class the function and print the message ?

@adhamenaya adhamenaya changed the title Can we subscribe abstract method Can we subscribe an abstract method Aug 22, 2016
@greenrobot-team
Copy link
Collaborator

As far as I know Java does not support annotation inheritance. So this does not work out of the box.

I suppose you could implement the method in your abstract class and then override it in child classes. If you register EventBus within the parent class, it should call the proper child method.

Like

abstract class Parent {  
        @Subscribe 
        public void sayHi(EventMessage em) {
        }
}
class ChildA{
        @Override
        public void sayHi(EventMessage em){
                 System.out.println("Hi I am ChildA");
       }
}

-ut

@greenrobot-team greenrobot-team self-assigned this Aug 22, 2016
@keluokeda
Copy link

keluokeda commented Sep 11, 2016

Java does not support annotation inheritance

if you need abstract method
your code can like this:

abstract  class Parent{

@Subscribe 
public void onEvent(Event event){
   doEvent(event);
}

protected abstract void doEvent(Event event);
}
class Child{
     @Override
     protected void doEvent(Event event){
   // do something
 }
}

@greenrobot-team
Copy link
Collaborator

See two workarounds above. Keeping this open for future reference if we ever want to support this. -ut

@greenrobot-team greenrobot-team removed their assignment Sep 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants