Michael Will
Posts: 3
Nickname: zmichael
Registered: Dec, 2002
|
|
Re: the inheritance and polymorphism
|
Posted: Dec 4, 2002 5:50 PM
|
|
> Are we not doing this by implementing a Interface and then > coding to the implementation ?
Inheritance of behaviour means inheriting interface AND implementation.
In java, you can only directly inherit from one baseclass, but you can implement multiple interfaces. So whenever you face a situation where you want to inherit the implementation too, you are forced to duplicate the code for it.
Assume there are two classes a,b which implement something unrelated each. now assume you want to implement classes c and d who also implement something unrelated, but both want to do what a and what b does without reimplementing the wheel.
illegal: - class c extends a,b - just use methods of a and b - class d extends a, b - d should not extend c when it has no is-a relationship legal: - class c implements ia, ib - +code for methods of a and b - then use that implementation. - class d implements ia, ib - +code for methods a and b duplicated - or we extend c but what if they have no real is-a relation?
Or you do create an extra "mixin" class that implements both interfaces, which you can then extend, practically extending both at once.
- class mixin implements ia, ib - class c extends mixin - class d extends mixin
I think that is how I would do it. Because you model "what a and what b does" as "mixin" and then extend that.
Michael
|
|