4

im trying to create my little library. But i have little problem. I cant right understand how to hide some classes/methods from public usage. Say i have structure :

-myPackage.com
  -classA  (public class example : public classA {...})
  -classB  (just a class example : class classB {...}

in this way when i add my aar file to android project. I can use from library only classA. classB hided(invisible for developer) and i cant call him (its ok). But classA can use functions from classB . Because its in same package. And its ok. So how correctly create classes in another packages ?

-myPackage.com
 -myHelpPackage
  -classC (public class example: public class classC {...}
  -classD (just a class example: class classD {...}
 -classA (public class example : public classA {...})
 -classB (just a class example : class classB {...}

in this way i have two public classes which developer can call

classA
classC

classD visible only for classA. So i cant call classD(functions) from classA. Main question : how to achive it ? I want to have only one classA to call. classC is ok. classD and classB should be invisible for developer. But visible only for classA. Anyone can help me with this ?

Peter
  • 2,480
  • 6
  • 39
  • 60

2 Answers2

0

If you don't specify any access level modifier, e.g.:

package myPackage.com

class classA {
}

then the class will be package-private by default. This means it is only visible to classes inside your package. See also the documentation by Oracle here

JakobJeremias
  • 86
  • 1
  • 5
0

This question has been answered in the comments by JakobJeremais, but in case it's not clear to anyone, or they skip over it because they only read top-level answers (understandable). If you have a package-private class in your aar, it will still be publicly accessible, so I would recommend nesting the class in whatever calls it, an you will be safe.

For instance, I had to extend EditText so that I could override just one method, and nothing else. Then I had to reference that from only one other class, but didn't want it accessible from whatever projects I added my library to. So I nested it, and now it's fine.

public class VisibleClass extends View {
    public static HiddenClass extends EditTextCompat { 
        @Override
        public void annoyingFunction() {
            //Disable this function
        }
    }
}

I should note, though: The compiler isn't happy about accessing it from layout, so I suppose it hasn't really helped, so I wouldn't recommend it, for now. If you have time to tinker, go ahead, though.

Sedge
  • 151
  • 1
  • 3