1
public interface foo {

    String ex(String a);
}

public class myclass implements foo {
    public String ex(String a) {
        //define the method
        return a;
    }
    public static foo getsome() {
        //have to return for example if I do ex("abc") return "123" but have to retrun the object of the interface o.O
    }
}

I don't know how to return an object of an Interface because I know that an object of interface cannot be implemented. On the other side, get methods of the all commands has no input. So what can I do?

Melika Barzegaran
  • 429
  • 2
  • 9
  • 25
HKing
  • 83
  • 1
  • 12
  • take a look at it http://stackoverflow.com/questions/7311274/attributes-member-variables-in-interfaces – Joe Doe Mar 20 '15 at 16:59
  • An "object of an Interface" is simply any instance of a class that implements said interface somewhere in its heirarchy. If you've actually got an object that implements that interface then you can be certain that the object in question *does* implement all of that interface's methods. Otherwise you wouldn't have been able to get the object to begin with. All you essentially need to do here is return an instance of `myclass` from the `getsome()` method. – JonK Mar 20 '15 at 16:59
  • Thank you @JonK,now I understand how to use my interface commands. – HKing Mar 20 '15 at 18:03

1 Answers1

2

I will call object, an instance of a class, for example:

private myclass myObject = new myclass();

This object (myObject) can be accesses as its class (myclass), any interface it implements (foo) or any class it extends (Object, because every class extends Object in Java). So the following are all valid:

public myclass getMyClass() { return myObject; }
public foo getMyFoo() { return myObject; }
public Object getMyObject() { return myObject; } 

So coming back to your code, if you want to use a static method:

private static myclass instance = new myclass();

public static foo getsome() {
     return instance;
}
Richard
  • 527
  • 3
  • 9
  • Thank you for you help,but I have the same problem because the return type of my command is String not Command for ex("abc") to do the getsome. – HKing Mar 20 '15 at 17:53
  • @HKing I'm sorry, but I didn't understand the problem you still have. The implementation of the conversion from "abc" to "123" is in the "//define the method" section in your original question, so myclass.getsome().ex("abc") will return "123". – Richard Mar 20 '15 at 18:10
  • if I return only the istance in getsome() eclipse give me an error like 'unraechble code'.. So I think I have to return istance.execute("abc") in getsome... no? but the problem is that return a String object,not the command object. – HKing Mar 20 '15 at 18:31
  • "unreachable code" is not caused by returning the instance, but by some code which will never get executed because it's after the return statement. If you return instance.execute("abc") the type is String, not foo. – Richard Mar 20 '15 at 18:41
  • Thank you man,I don't know that... you are my hero! can you tell me why I have to return only the istance? – HKing Mar 20 '15 at 18:46
  • You have to return an object of the correct type. Because you made `getsome()` static I assumed you want to return static object; otherwise you could do: `public static foo getsome() { return new myclass(); }` but that would create a new object for every call to `getsome()`. – Richard Mar 20 '15 at 19:35