0
what is protected keyword in the classes?
3 Answers
+ 4
any protected attribute can be accessed by the class itself and any class that inherit that class
but cannot be accessed otherwise
+ 2
protected method or attribute can only be accessed withing parent class or subclass e.g let's say you have
Class Foo {
protected String name = "some text";
public String void myFunc(){
//code here
}
}
class Bar extends Foo{
public static void main(String args[]){
System.out.println(new Foo().name);
}
}
//Output: some text
but if you try to access name attribute in class that is not a subclass of Foo you will get error.
0
thank you all my friends!