Replacing Multiple Inhertance with delegation

I was going through “Object Oriented Modelling and Design” by James Rumbaugh et al and it said that in languages where multiple inheritance is not supported like Java three mechanisms can be used as workarounds

  1. Delegation using aggregation of roles
  2. Inherit the most important
  3. class and delegate the rest Nested Generalization

I couldnt understand the examples given in the text. Could any one please explain this with an example. I know that “diamond problem” is a problem within multiple inheritance and java supports multiple inheritance of interfaces.

My observation is that a lot of folk misuse multiple inheritance without working through composition type questions.

The “classic” OO example of a car leads to this problem. For example, we’ll say that a car breaks down to the following parts:

  • it’s a vehicle
  • there are tires
  • there is an engine
  • there is a chassis
  • there are seats
  • and so on…

so we’ll define a class that looks like this:

 public class Car : Vehicle, Tires, Engine, Chassis, Seats {}

All well and good, but it presents some significant problems with encapsulation and constructors / destructors. And it doesn’t accurately represent what a car looks like in the real world.

If we start asking composition questions then we’ll see some nuances here that weren’t at first obvious.

  • is a car a vehicle? (yes)
  • is a car a tire or set of tires? (no, it has a set of tires)
  • is a car an engine? (no)
  • is a car a seat? (no)
  • etc …

So now our class can look something like this:

public class Car : Vehicle
{
   public Tires tires;  //ie, myCar _has_ a set of tires.
   protected Engine engine;
   private Chassis chassis;
   protected Seats seats;
}

Which is also all well and good. But now we start scratching our heads and say “you know, I really wish that when I instantiate a car that it has to have tires, an engine, and a chassis.”

So now we can turn Tires, Engine, and Chassis into interfaces and we’ll have the following class. I’m prefixing the interfaces with “I” to make the distinction more clear.

public class Car : Vehicle, ITires, IEngine, IChassis
{
   protected Seats seats;
}

Interfaces are like contracts. When a class implements an interface, it’s guaranteeing it will provide certain methods and behaviors.

And now things really are all well and good. Our model of the car accurately reflects the real world representation of the same Object, including some guarantees about the resemblance between the two courtesy of the Interfaces. We have avoided the icky issues that multiple inheritance can create.

That’s not to say that multiple inheritance is always bad. However, it’s not really needed most of the time which is why languages like Java and C# don’t allow for multiple inheritance.

3

Java does not provide multiple inheritance. Interfaces are in fact not inherited they are implemented/realized. Inheritance is used to extend a class static and dynamic characteristics, with composition (delegation, aggregation) the behaviors of a class are extended by adding adding classes instances (attributes, references) it can use to perform the services it provides. For instance if you have a Class for a thread which needs a socket you can make the Thread class extending the Socket class (very bad decision) and use the method it inherits or make the Thread class owning a Socket object it can use and delegate to it the socket stuff. Same behavior very different architecture.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật