Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing a library as a package private

Tags:

java

android

I know we can create a package private class in java. So the class is Internal and only accessible in the specified module:

class MyPackagePrivateClass
{
    ...
}

Now I am developing an android library which i named LibraryA and I wish to use an existing LibraryB in my own LibraryA. How is it possible to prevent user of LibraryA from using the LibraryB directly?

Is there any concept like package private library or anything like that?

Update (For those who ask 'why do I need this?')

In LibraryB there are methods like this:

public QueryBuilder select(String... columns);

But I strongly am of the view that we should use Type-Safe Enum pattern and prevent users from passing strings to these methods(Considering maintenance and refactoring issues). So I decided to wrap those methods in LibraryA:

public TypedQueryBuilder select(Column... columns) {
        queryBuilder = queryBuilder.select(toString(columns));
        return this;
    }

Therefore users of my library should use the Typed methods I provided (Column is a type safe enum here). But if they have access to the original method they may use those instead and I tend to forbid this.

like image 938
a.toraby Avatar asked Nov 19 '22 08:11

a.toraby


1 Answers

In Java, with polymorphism, you can't hide a public method of an extended class.

I think that you could archive your goal with Facade Pattern: hide all the complex logic and, in this case, control the access and, if needed, implements some interfaces.

like image 69
Manuel Spigolon Avatar answered Dec 11 '22 04:12

Manuel Spigolon