3

I want to define a variable that implement 2 interface. Imove and Ijump are 2 interface.

class b implements Imove,Ijump {...}
class a implements Imove,Ijump {...}
Imove,Ijump player = new a();
Imove,Ijump player = new b();
player.Jump();
player.Move();
asked Dec 6, 2020 at 13:48
2
  • You refer to the object instance either by it's class or one of the interfaces it implements, e.g. a player = new a(); or Imove player = new a(); etc Commented Dec 6, 2020 at 13:59
  • 1
    Alternatively, if you need something that is Imove and Ijump, you could create an interface Imoveandjump extends Imove, Ijump {} and use this. --- Some remarks on your code: class-, interface- and enum-names should always be written in CamelCase (a -> A, Imove -> IMove, Ijump -> IJump), while methods should be written in camelCase (Jump() -> jump(), Move() -> move()) --- In general, interfaces should not be prefixed (Imove -> Move, IJump -> Jump). Commented Dec 6, 2020 at 14:04

1 Answer 1

3

interface can inherit from (multiple) other interface with extends.

Define third interface.

interface MoveAndJump extends IMove,IJump{};

Use that third interface.

MoveAndJump player = new a();
player.jump();
player.move();
Basil Bourque
347k130 gold badges951 silver badges1.3k bronze badges
answered Dec 6, 2020 at 14:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.