This may be a silly question as I am new to android programming. I have imported a .aar library in my android project where I am trying to access a protected final synchronized method in one of the class present in that library. However, I am able to refer other methods present in the class which are not synchronized/final. When I open the source of the library from which I created the .aar file, I am able to see the method but unable to use it after importing it.
Can you please tell what is going wrong here?

- 1,092
- 12
- 32
-
1https://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-and-private – shmosel Aug 25 '17 at 22:34
3 Answers
Importing has nothing to do with accessibility and visibility of fields and methods of the imported class, it is just a shortcut for not typing the full class name every time you use the class.
To access an imported method or field of a certain class, the modifier preceding them should be marked as public.
Since the method you tried to access is marked as protected, the method can be accessed only if you are trying to access it within the subclass that extended class BaseActivity, or if you are accessing it from the class of the same package.
The solution is to extend the BaseActivity and either use the method within your customized method like this:
public class SomeOtherActivity extends BaseActivity{
public void someOtherMethod(Runnable task, long delayMillis){
super.queueEvent(task,delayMills);
}
}
This way the method you declared provides a public interface to other developers that wants to use your specific version of method that queues events.
However, this is not done through polymorphism nor with composition, so you might have to make sure that you really need this method or if there's another existing solution, since there's a reason that the original method is marked as final and protected which might cause other problems if not dealt with care.
Introduction to access modifiers can be found here:
The Java tutorials - Controlling Access to Members of a Class
On the other hand, this problem has nothing to do with "synchronized" and "final"
Introduction for the keyword synchronized can be found here:
The Java tutorials - Synchronized Methods
Introduction for the keyword final can be found here:
The Java tutorials - Final Classes and Methods

- 187
- 10
When you say "use" do you mean call/invoke or override?
A. protected methods are only visible to the package and subclasses
B. final methods cannot be overridden
With A and B the only thing you can do with that method is call it in your subclass.

- 2,445
- 16
- 17
Protected methods can be called only by classes that extend the main class.
To bypass this error that you are getting, you can create a class that extends your BaseActivity
class and then a public method that calls the private one.
Something like this:
class MyBaseActivity extends BaseActivity {
public final synchronized void myQueueEvent(Runnable task, long delayMillis) {
super.queueEvent(task, delayMillis);
}
}
Now you can use it by calling: MyBaseActivity.myQueueEvent(...);

- 2,034
- 15
- 22