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
Alberto Zanovello
1581 silver badge8 bronze badges
1 Answer 1
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
Alberto Zanovello
1581 silver badge8 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java
a player = new a();orImove player = new a();etcImoveandIjump, you could create aninterface Imoveandjump extends Imove, Ijump {}and use this. --- Some remarks on your code: class-, interface- and enum-names should always be written inCamelCase(a->A,Imove->IMove,Ijump->IJump), while methods should be written incamelCase(Jump()->jump(),Move()->move()) --- In general, interfaces should not be prefixed (Imove->Move,IJump->Jump).