user4667647
user4667647

Reputation: 11

Calling class's methods into an interface

I have created some interface such that:

public interface A{

}

and i would like to call the method a that I have already implemented in class B in interface A such that:

public class B{
    public boolean a(){
       return true;
    }
}

public interface A{
     public void call {
        a();
     }
}

without any errors, any help please?

Upvotes: 1

Views: 133

Answers (4)

gregdim
gregdim

Reputation: 2071

If you are using any java version before 8, then stick with the answers of @tinker and @Davis Broda. They provide better design since they do not couple your interface to the B class. If you insist however, in java 8 you can have default method implementations as well as static methods in an interface.
If your method is for inheritance then you have to use a default method. Add the default keyword:

default void call() {
   ...
}

Now the problem is how to get a reference to the class in order to call the method since you cannot have instance fields in interfaces. You have two choices:

Pass the object of B as a method parameter:

   public interface A{
     default void call(B b) {
          b.a();
     }
   }

or make the method in B static

   public interface A{
      default void call() {
          B.a();
      }
   }

If your method is not for inheritance but just a utility than you can make it static as :

public interface A{
   public static void call() {
       B.a();
   }
}

Upvotes: 2

fps
fps

Reputation: 34450

Your question is not very clear, but if I'm guessing right, you want interface A to be kind of a generic caller.

If you're using Java 8, you can achive that using a method reference:

public class B {

    public boolean a() {
        return true;
    }
}

public interface A<T> {

    default T call(Supplier<T> s) {
        return s.get();
    }
}

public class AImpl
    implements A<Boolean> {
}

public class Sample {

    public static void main(String[] args) {

        AImpl a = new AImpl();
        B b = new B();

        boolean result = a.call(b::a);

        System.out.println(result); // true
    }
}

This uses Supplier<T> because your method a() in class B returns a boolean and does not receive any arguments.

Upvotes: 0

tinker
tinker

Reputation: 1406

I agree with @Davis Broda's answer, there is no way to have a method definition in an interface. But I have another way to address this.

You can have the interface and then have an abstract class implement this interface, and then have all other classes extend the abstract class. The abstract class doesn't have to extend the class from where you want to call the method, you could call it from an instance of that class too.

public interface A {
    void caller();
}

public class B {
    public void callMe() {
    }
}

public class AbstractA implements A {
    private B b;
    public AbstractA(B b) {
        this.b = b;
    }

    @Override
    public void caller() {
        b.callMe();
    }
}

This way, all implementations of AbstractA will be able to call B's callMe method. And you can access this directly from the interface using this code:

A anInstance = someInstance;
anInstance.caller();

Upvotes: 1

Davis Broda
Davis Broda

Reputation: 4125

What you want to do is strictly speaking impossible, as you cannot define method implementations in an interface. You can get something similar by defining an implementation of the interface that extends B. Hopefully that is close enough.

public class AImplementation extends B implements A{
    public void call(){
        a();
    }
}

Upvotes: 5

Related Questions