Search code examples
javaoopinheritancemultiple-inheritancelibrary-design

Inheritance in Java: naming collision case


This question is not only about Java, it is also about another object oriented languages. I designing simple UI library and it has got a special interface called Drawer which contains abstract methods for drawing controls and primitives on some graphical surface where we can set pixels. It designed to be built on the top of some simple graphics platform. And just for now I use AWT drawing engine. The thing is I created class AWTDrawer which extends java.awt.Canvas and implement Drawer and here is a naming collision: both ancestors have SetColor, getColor, drawLine methods. How can I determinate what ancestor's method I want to use?


Solution

  • This is generally a fault in your design.

    From your description you have implemented an is a relationship between your Drawer class and the java.awt.Canvas class by extending it.

    It seems likely that you actually want a has a relationship.

    To solve this, instead of extending java.awt.Canvas, hold a local Canvas instance variable and use it in your Drawer class.