1

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?

Kuznetsov S.A.
  • 169
  • 2
  • 11
  • 1
    When creating an interfaces, its a good idea to start the name with an I so you know at first sight it is an interface (for example IDrawer). – Katianie Jun 01 '18 at 13:35
  • 1
    If `AWTDrawer` **extends** `java.awt.Canvas`, then the latter must be a class. Unless the signatures of methods are the same, then you should be fine if your instances are declared as `Drawer` type. – ernest_k Jun 01 '18 at 13:36

2 Answers2

3

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

I would do it by using Full Qualified Names, from the chosen ancestor.

Instead of calling just

methodName

You try something like:

java.package.subpackage.class.method

Other languages like C#, same idea but they call it Namespaces

Pvic
  • 101
  • 1
  • 10
  • Unfortunately, Java perceives such method call as calling of the static method, but it can't find such static method cause there is no such. – Kuznetsov S.A. Jun 01 '18 at 13:44