1

Suppose i have MainActivity class in which i have two buttons, when B1 is pressed it calls all the methods of Operations_class_1 which this class implements from Interface and same scenario goes for when i pressed B2 but with Operations_class_2

Public interface Myinterface(){
void method1(); void method2() }

Public Operations_class_1 extends Activity implements Interface
{
method1(){
//calculations perform here
 }
method2(){
//calculations perform here
 }
 }

But my question is how to call the these Operations classes methods from MainActivity class buttons

Public class MainActivity extends Activity{
//how to call the methods of those classes by making object of Interface class
} 
DAIRAV
  • 723
  • 1
  • 9
  • 31
robinHood013
  • 209
  • 1
  • 2
  • 15
  • 3
    Possible duplicate of [How to use one interface in multiple class in java?](https://stackoverflow.com/questions/37482710/how-to-use-one-interface-in-multiple-class-in-java) – AskNilesh Apr 19 '18 at 04:16
  • https://stackoverflow.com/questions/31786831/implementing-one-interface-using-multiple-classes – AskNilesh Apr 19 '18 at 04:16
  • @NileshRathod actually i have seen the mentioned link , but i couldn't understand much. that's why i have asked this again. am sorry but i am not clear from how to call the those two classes methods from MainActivity class – robinHood013 Apr 19 '18 at 04:25
  • in your main activity you have to implement interface like yo did in `Operations_class_1 ` class. – Hemant Parmar Apr 19 '18 at 04:47
  • you need to make object of both class Operation1 and Operation2 in MainActivity. then implement Interface in MainActivity. then onClick of button1 call you method1 and onclick of button2 call method2 using interface object. – Hitesh Sarsava Apr 19 '18 at 04:58
  • @HiteshSarsava is there any other way to call the methods of these classes, coz if i have many classes , it would be messy code if i make object of every class in mainActivity – robinHood013 Apr 19 '18 at 07:34
  • you can make every method static for that classes and access them with Class name instead of creating object of class @ArslanAliAwan – Hitesh Sarsava Apr 19 '18 at 07:36

1 Answers1

1

//how to call the methods of those classes by making object of Interface class

In Java, you can instantiate classes (rather than interfaces) and use the fact they implement the interface to refer to them as that type.

Public class MainActivity extends Activity {
    MyInterface opClass1 = new Operations_class_1();
    MyInterface opClass2 = new Operations_class_2();

    ...

    final Button button = findViewById(R.id.b1);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             opClass1.method1();
             opClass1.method2();
         }
     });
}

Because the class you're instantiating is an Activity it also has a lifecycle and all the stuff that comes along with Activities. I recommend checking out this guide: https://developer.android.com/guide/components/activities/index.html

TisSarah
  • 101
  • 1
  • 3