views:

864

answers:

8

I have created two beans

class BackPageBean{
   String backPage = null;
  :
  :
  :
}


class InformationMessageBean{
   String informationMessage = null;
  :
  :
  :
}

Now, if a class is backpage aware then it will extend backPageBean, or if it need to show some kind of message then it extends InformationMessageBean.

class BackPageAware extends backPageBean{
}

class InfoMessAware extends InformationMessageBean{
}



    someFunction () {
       if ( theObject instanceOf backPageBean ) {
              prepareTheBackPage ( theObject.getBackPage() );
       }

       if ( theObject instanceOf InformationMessageBean ) {
              showtheInformation ( theObject.getMessage() );
       }

   }


Now the problem is, if i want to have a bean which is both BackPageAware as well as InformationAware then, as we don't have multiple inheritance, what should be the approach?

+9  A: 

use interfaces:

interface InfoMessAware {

     String getMessage();
}

interface BackPageAware {

     String getBackPage();
}

class MyBean implements InfoMessAware, BackPageAware {

     String getMessage() {
         return "message";
     }

     String getBackPage() {
         return "backPage";
     }
}

then replace instanceof with standard method calls.

dfa
That certainly means i will have to have the (same)definition of getbackpage and getMessage in all class which are going to implement these interface.String getMessage() {return this.message; } // we will have a setter alsoString getbackPage() {return this.backPage; } // we will have a setter also
Rakesh Juyal
unfortunately Java has multiple inheritance only for interfaces
dfa
+1  A: 

Sounds like you need to use interfaces to define the multiple behaviours.

RichardOD
A: 

Declare BackPageAware and InformationAware as interfaces, create abstract classes that implement those interfaces to provide the default functionality. Any class that needs to be only one of the two interfaces can inherit the corresponding abstract class. A class that needs to be both can inherit one of the abstract classes and implement the other interface, or implement both interfaces, whatever. If there is no default functionality, then you don't even need the abstract classes.

Sean Nyman
If a class needs both behaviours I'd implement the interfaces and delegate to the default implementations. Has-A rocks :)
willcodejavaforfood
will you please elaborate willcode?
Rakesh Juyal
Just like Darth Eru says you create the two interfaces and the two default implementations. When you have a bean that needs both of the behaviours you have that class implement the two interfaces but you also create variables of the default implementations. This way you still dont need to duplicate any code.
willcodejavaforfood
A: 

You can't extend multiple classes but you can implement multiple interfaces. Remember that when you apply an interface to a class, the object of the class has a IS-A relationship with the interface.

omgzor
+6  A: 

The problem you are describing begs the usage of composition, not inheritance. The class being BackPageAware means it knows about that class/functionality. Inheritance means it IS a BackPage. You have described a HAS A relationship.

As has been said many times now, use interfaces to define the contracts for retrieving the information that the object HAS.

Robin
+4  A: 

Just to clarify my comment.

Just like Darth Eru says you create the two interfaces and the two default implementations. When you have a bean that needs both of the behaviours you have that class implement the two interfaces but you also create variables of the default implementations. This way you still dont need to duplicate any code.

    interface InfoMessAware {

         String getMessage();
    }

    interface BackPageAware {

         String getBackPage();
    }

class DefaultInfoMessAware {
         String getMessage() {
             return "message";
         }
}

class DefaultBackPageAware {
         String getBackPage() {
             return "backPage";
         }
}

    class MyBean implements InfoMessAware, BackPageAware {
         private InfoMessAware messAware = new DefaultInfoMessAware();
         private BackPageAware backPageAware = new DefaultBackPageAware();

         String getMessage() {
             return messAware.getMessage();
         }

         String getBackPage() {
             return backPageAware.getBackPage();
         }
    }
willcodejavaforfood
+1  A: 

Your two original classes should be Interfaces, each with a method on them which retrieves the information that implementations should return.

public interface BackPageBean {
   public String getBackPage();
}


public interface InformationMessageBean {
   public String getInformationMessage();   
}

If you want a class to implement both BackPageBean and InformationMessageBean you simply do this:

public MyBean implements BackPageBean, InformationMessageBean {

  public String getBackPage() {
    return null;
  }

  public String getInformationMessage() {
    return null;
  }
}

Generally speaking, you should avoid extending non-abstract classes whenever you can, it leads to all sorts of problems. Instead, try using composition instead of inheritance where concrete classes are involved, and otherwise, try and stick to interfaces and the occasional Abstract class.

Jon
A: 

As discussed in other answers multiple inheritance can be simulated using interfaces and composition, at the expense of having to write a lot of boilerplate code. There are however a number of projects around that can automate this either at compile time (via a pre-processor) or at runtime eg jmixin.

someguy