-3

I am confused with access specifiers in Java.. I have a code with 2 different packages in Java.. But it displays an error every time I run it.. Here's is the code for the class which is calling the class using import from another package..

package p2;

import p1.Testing;

class Pqr extends Testing{ // extending the class 
    void hey(){
        System.out.println("Something");
    }
}

class Xyz{
    public static void main(String args[]){
    Pqr t1 = new Pqr(); // Class from another package.
    System.out.println(t1.find("Mississippi","p"));
    t1.hey();
    }
}

Code for class which is being subclassed from package p1..

package p1;
class Testing{
    protected static boolean find(String a,String b){ // Protected specifier
        boolean ans = false;
        for(int i=0;i<a.length();i++){
            String m = a.charAt(i) + "";
            if( m.equals(b)){
                ans = true;
            }
        }    

    return ans;
}

    public static void main(String args[]){

    // Main Class

    }

}

But when I run the code i get an error "Testing is not public in p1; cannot be accessed from outside package"..

I learned in this thread that we can use protected method between different packages but by extending it by another class.. In Java, difference between package private, public, protected, and private

Thanks in advance.

Akshit Gupta
  • 109
  • 1
  • 8
  • 3
    `public class Testing` .. Your CLASS itself is not public. – Brandon Jan 12 '18 at 21:50
  • 3
    You're not calling method the from a subclass. You're just calling it on *an instance of* the subclass. Except that it's static, so you're not really doing that either. `t1.find(...)` is a confusing equivalent to calling `Testing.find(...)`. – shmosel Jan 12 '18 at 21:51
  • @AniketSahrawat Sir I read this in the thread that you can use method outside package using protected specifier.. – Akshit Gupta Jan 12 '18 at 21:54
  • @AkshitGupta Only from subclasses. `Xyz` is not a subclass of `Testing`, so `protected` doesn't grant access. Only `public` will. – Andreas Jan 12 '18 at 22:18
  • @Andreas Sir, class pqr extends Testing making it the subclass of Testing.. Can I call Testing methods in this class? – Akshit Gupta Jan 12 '18 at 22:33
  • @AkshitGupta Did you try? What happened? This is called research, and you should do research before asking questions here. – Andreas Jan 12 '18 at 22:41
  • @Andreas Yes sir, i did try that and as it turns out I can call Testing methods in pqr provided the find method in Testing is either public or protected.. Otherwise it raises an error.. Thanks.. – Akshit Gupta Jan 12 '18 at 22:45

1 Answers1

1

You don't have an access to the top-level class (in your case it's Testing), so you can't have an access to its members, no matter what are its access modifiers. You need to make Testing public to make its protected members visible from outside of its package to classes extending Testing. You can read more about it in Java tutorial. Here is a part about access modifier of Testing class:

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package

And the part about your find() method:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

But to make use of protected members of class contained in another package, the class itself containing it need to be visible in another packages (it needs to be public).

Take a look at this Java tutorial for more informations about access modifiers: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21