• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Java Inerface

How to resolute multiple Inheritance with Interface in java
0
markzhang2000
Asked:
markzhang2000
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now