Extending a specific base hierarchy class

Say I used a library for forms that suggested:

class Shape class Circle extends Shape class Square extends Shape 

but I want to introduce the concept of red shapes. I cannot change the library, but I can create:

 class RedShape extends Shape 

however, this cannot be easily extended to RedCircle , as it cannot distribute both RedShape and Circle .

I don't think the decorator pattern works here, but is there a way to achieve this?

+5
source share
2 answers

A short answer can be found in Effective java from Josh Bloch, Paragraph 16: Positive composition over inheritance.


EDIT : Composition is what you will need to do, really. Multiple inheritance is not allowed with java . People have tried . They have a fit . They are writing articles about this. They even use code generation or proxy generation tools (not a jdk proxy in your case, it cannot make a proxy class for a class), but nothing is as elegant as you would like.

+1
source

Define the Colorable interface using the getColor and setColor methods, and color forms implement it.

0
source

All Articles