protected access specifier allows access to the super class(i.e., the class which is declared as protected) and provides access only to the first level of inheritance(i.e., the base class which is first derived from the protected super class). isn't it?.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

protected access specifier allows access to the super class(i.e., the class which is declared as protected) and provides access only to the first level of inheritance(i.e., the base class which is first derived from the protected super class). isn't it?..

15th Aug 2016, 1:39 AM
Gowtham krvz kv
Gowtham krvz kv - avatar
3 Answers
0
The protected method is accessible from within its package, and also from outside its package by subclasses of the class containing the method. The protected data member is accessible within its package, and also from outside its package by subclasses of the class containing the data member. Source: java 7 pocket guide book.
15th Aug 2016, 3:36 AM
Tiger
Tiger - avatar
0
could you give me an example?.
15th Aug 2016, 3:39 AM
Gowtham krvz kv
Gowtham krvz kv - avatar
0
class Test{ protected int age; // age field is accessable from all classes in the current package and all subclasses from other packages. } public class Program { public static void main(String[] args) { Test t = new Test(); t.age = 24;// age field is accessable because both classes (Test and Program) in the same package System.out.println(t.age); } }
15th Aug 2016, 3:54 AM
Tiger
Tiger - avatar