I parse the finished application under android, the following lines of code cause confusion.

public interface Interface_1 { void interface_1 (); } public interface Interface_2 { void interface_2 (); } public class MyClass_1{ public Interface_1 interface_1; public MyClass_1 (Interface_1 interface_1){ this.interface_1= interface_1; } } public class MyClass_2{ public Interface_2 interface_2; public MyClass_2 (Interface_2 interface_2){ this.interface_1= interface_1; } } public class MainActivity extends AppCompatActivity implements Interface_1 , Interface_2 { new Interface_1 (this); new Interface_2 (this); @Override public void interface_1() { //Реализация метода } @Override public void interface_2() { //Реализация метода } 

Please explain why this works, because as far as I understand, in this case, this is a link to MainActivity, and when creating objects, you need to pass links to interface_1 and interface_2 objects.

    1 answer 1

    as far as I understand, in this case, this is a link to MainActivity

    You understand correctly.

    when creating objects, you need to pass references to the interface_1 and interface_2 objects

    And that's all right.

    why does it work

    Because the MainActivity implements Interfaces Interface_1 and Interface_2 . That is, if by simplicity, then MainActivity is both Interface_1 and Interface_2 and AppCompatActivity and Context and many more that all its parent classes inherit or implement.

    • Understood thanks. - Denis