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.