Furqan
Furqan

Reputation: 9

Import a class from a hierarchy of packages

If there's a hierarchy of packages(i.e pckg1.pckg2.pckg3) and each of them has a same class(i.e Abc), then how do i import the class Abc from pckg1 ?

Is the statement import pckg1.pckg2.pckg3.Abc ambiguous as Abc is present in all the three packages.

Upvotes: 0

Views: 617

Answers (4)

Brian Risk
Brian Risk

Reputation: 1449

As mentioned you would:

import pckg1.Abc;

But I would also do the full package path when defining variables to avoid ambiguity. For example:

pckg1.Abc abc = new pckg1.Abc();

Upvotes: 0

Michael
Michael

Reputation: 44090

There's no such thing as a package hierarchy actually.

pckg1.pckg2 knows nothing about and inherits nothing from pckg1. Indeed, pckg1.pckg2 can exist without there even being a pckg1. It's basically just a naming convention to help logically order things.

pckg1.pckg2.pckg3.Abc is therefore not ambiguous. It will come from the package that you currently consider the lowest in the hierarchy, pckg3.

That said, there is no such thing as pckg3. There is pckg1, pckg1.pckg2 and pckg1.pckg2.pckg3 but they could just as easily be called cat, banana and magic.

Upvotes: 3

talex
talex

Reputation: 20436

import pckg1.pckg2.pckg3.Abc is not ambiguous. It refer to specific class. In this case Abc from pckg1.pckg2.pckg3 package.

In java there is no package hierarchy. It is only look like pckg1.pckg2 and pckg1.pckg2.pckg3are related, but in fact there are no relation between them (except file structure where .class files are stored, but it is not part of language).

Upvotes: 1

leaqui
leaqui

Reputation: 543

import pckg1.Abc;

Look packages as windows folders.

Upvotes: 0

Related Questions