Link to home
Start Free TrialLog in
Avatar of markzhang2000
markzhang2000

asked on

Java Inerface

How to resolute multiple Inheritance with Interface in java
ASKER CERTIFIED SOLUTION
Avatar of Ravindra76
Ravindra76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbormann
mbormann

Mutiple inheritance are used as Callbacks ,see this is how it occurs in Java and C++.

see
https://www.experts-exchange.com/bin/Q.10212921
for refernce,had a good discussion going there



              interface InterestingEvent
              {
                  // This is just a regular method so it can return something or
                  // take arguments if you like.
                  public void interestingEvent ();
              }

              class EventNotifier
              {
                  private InterestingEvent ie;
                  private boolean somethingHappened=false;

                  public EventNotifier (InterestingEvent event)
                  {
                  // Save the event object for later use.
                  this.ie = event;

                  // Nothing to report yet.
                  //ACTUALLY NO NEED here initialize already upstairs
                  somethingHappened = false;
                  }

                  public void setSomethingHappened()
                  {
                      somethingHappened=true;
                  }
                  public boolean isNotError()
                  {
                      if(true)//some condition
                          return true;
                      else
                          return false;
                  }
                  public void doWork ()
                  {
                      if (somethingHappened)
                      {
                          // Signal the even by invoking the interface's method.
                          ie.interestingEvent ();
                      }    
                  }
              }

              public class CallMe implements InterestingEvent
              {
                  private EventNotifier en;

                  public CallMe ()
                  {
                      // Create the event notifier and pass ourself to it.
                      en = new EventNotifier (this);
                      if(en.isNotError())
                      {
                          en.setSomethingHappened();
                          en.doWork();
                      }
                      else
                      {
                          System.out.println("**** ,Exiting");
                          System.exit(1);
                      }
                  }

                  // Define the actual handler for the event.
                  public void interestingEvent ()
                  {
                      // Wow! Something really interesting must have occurred!
                      System.out.println("CallMe's interestingEvent () called from Object of
              EventNotifier !");
                  }

                  //...
                  public static void main(String [] args)
                  {
                      new CallMe ();
                  }
              }

              see if u find this useful from
              http://www.primenet.com/~jakubik/source-simple.html 

              Callbacks in C++ simple solution

              #include <iostream.h>
              class Button;
              class CdPlayer
              {
              public:
                void buttonPushed(Button* pButton_)
                {
                  if (pButton_ == _pPlayButton)
                    this->playButtonPushed(pButton_);
                  else if (pButton_ == _pStopButton)
                  this->stopButtonPushed(pButton_);
                }
               
                void setPlayButton(Button* pButton_) {_pPlayButton = pButton_;}
                void setStopButton(Button* pButton_) {_pStopButton = pButton_;}
                void playButtonPushed(Button*) {cout << "PLAY" << endl;}
                void stopButtonPushed(Button*) {cout << "STOP" << endl;}

              private:
                Button* _pPlayButton;
                Button* _pStopButton;
              };
              //----------------------------------------
              class Button
              {
              public:
                Button(CdPlayer* pCdPlayer_): _pCdPlayer(pCdPlayer_){}
                void push() {_pCdPlayer->buttonPushed(this);}
              private:
                CdPlayer* _pCdPlayer;
              };
              //----------------------------------------
              main()
              {
                CdPlayer aCdPlayer;
                Button playButton(&aCdPlayer);
                Button stopButton(&aCdPlayer);
                aCdPlayer.setPlayButton(&playButton);
                aCdPlayer.setStopButton(&stopButton);

                playButton.push();
                stopButton.push();

                return 0;
              }
Hi markzhang2000.

  Are you clarified?.

 
see for a very good solution to multiple inheritance
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10237885