Java Inerface

How to resolute multiple Inheritance with Interface in java
markzhang2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravindra76Commented:

Hi markzhang2000,

 Java provides partial support for multiple inheritance.

Multiple inheritance means exteding two base classes. Since java allows extending single base class, you can use  interfaces. Now you are able to implement one or more interfaces.

class classA extends classB implements interface1,interface2,interface3,....interfacen {


}

In multiple inheritance, you are extending multiple base classes. Here instead of multiple base classes, implement multiple interfaces.

But in interfaces, you have no variables. In my view variables in sense means the thing which will allowed to change. But interface "variables" are all constants.Variables define state. In interfaces, since variables are constants, no change in state.

In c++, you will get the state of base object variables because you have no restrictions.
So you can not get 100% multiple inheritance in java. Interface will allow grouping of methods.


IF you have doubt, how can i use multiple interfaces? I can use single interface with all methos.


Here an example to define multiple interfaces.

public interface1 {

 public void method1();
 public void method2();

}

public interface2 {

 public void method1();
 public void method2();

}

public interface3 {

 public void method1();
 public void method2();

}

class A implements interface1,interface2 {


}

class B implements interface2,interface3 {


}

In this case, class A don't want methos in interface3 and class B do't want methos in interface1.

That's why multiple interfaces came.

Best of luck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
Mutiple inheritance are used as Callbacks ,see this is how it occurs in Java and C++.

see
http://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;
              }
0
Ravindra76Commented:
Hi markzhang2000.

  Are you clarified?.

 
0
mbormannCommented:
see for a very good solution to multiple inheritance
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10237885 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.