Can anybody explain me why do we need "protected" word? If I understand correctly,
default access: available in classes within the same package.
protected access: default access in same package + available to inherited classes (sub-classes) in any package. Basically, we get the same default access in same package.
So when should I use it? Just for the style of your code? To mark it that you are going to work with it from perspective of inheritance?
Thank you.
package firstPack;
public class First {
protected int a;
protected void Chat(){
System.out.println("Here I am");
}
}
package secondPack;
import firstPack.First;
public class Second extends First{
public static void main(String [] args){
First f=new First();
// f.Chat();
// System.out.println(f.a);
}
}
I used this code to test it. It didn't work.