简体   繁体   中英

Java alternative to multiple inheritance

Since I know Java doesn't support multiple inheritance, I'd like to know how could I implement this design in Java:

Class StudentModel extends BaseModel{
   public void doSomething(){};
}
Class ParentModel{
   protected List<BaseModel> children = new List<BaseModel>();
   public void addChild(BaseModel child){
      children.add(child);
   }
   // and other parent-children related functionality
}

Class ConcreteParentModel{
    // should contain both StudentModel and ParentModel functionalily
}

Thanks!

You should use an adapter pattern

In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM