0

Is there a possibility, to use two classes which have got equal class names not having to directly specify from which package one of them comes from?

For example:

using com.org.TreeView;

...
TreeView tv = new TreeView();
reg.org.TreeView tvOrg = new reg.org.TreeView();

I don't want to every time specify that the other TreeView comes from reg.org.

What I would like to do is, to specify some variable, which takes the type of the reg.org.TreeView and every time I need to create the reg.org.TreeView I use this variable and not to whole type. Using the full path of the class can be annoying when the package root is huge.

N.Zukowski
  • 600
  • 1
  • 12
  • 31
  • A decorator might work – Hovercraft Full Of Eels Oct 22 '16 at 18:48
  • 1
    Check this question, it answers it perfectly http://stackoverflow.com/questions/2447880/change-name-of-import-in-java-or-import-two-classes-with-the-same-name – Josep Prat Oct 22 '16 at 18:49
  • @JosepPrat: Java isn't Python. – Makoto Oct 22 '16 at 18:52
  • My first thought was to write a subclass that extends `TreeView` (assuming it isn't `final`), and rewrite all the constructors using `super(...)`. There's still a problem if any of the public methods take `reg.org.TreeView` parameters, or return `reg.org.TreeView` results. If that isn't a problem, then you get the methods for free. (Except static methods.) – ajb Oct 22 '16 at 18:53
  • Well, let's back up a step. How do you have a scenario in which you can have ambiguous class names? Is this a code smell indicating that you should probably consolidate responsibilities somewhere? – Makoto Oct 22 '16 at 18:54
  • @Makoto how do you know _he_'s responsible for those class names? I run into this problem when using other people's libraries. – ajb Oct 22 '16 at 18:54
  • @ajb: I *don't* know, which is why I'm asking. To your point about third-party libraries sometimes introducing this: yes, that's true, but a layer of abstraction can solve that particular case should that be necessary. – Makoto Oct 22 '16 at 18:55
  • @Makoto please read the question I linked, they ask if a feature is available in Java. Casually this feature is qualified imports, which casually Python has. The question linked is about Java – Josep Prat Oct 22 '16 at 19:22

1 Answers1

-3

not because it is ambiguous, you have to specify the class package.