+ 1

access protected variable in another package in java

hi i have two pkg (protect,otherpkg) otherpkg package: package otherpkg; public class opkg { protected int m = 0; } and , protectpkg : package protect; import otherpkg.*; public class Protect { public static void main(String[] args) { Myclass myclass = new Myclass(); } } class Myclass extends otherpkg.opkg { Myclass() { otherpkg.opkg p = new otherpkg.opkg(); p.m = 70; System.out.println(p.m); } } but i got error in line: p.m = 70; why?

21st Aug 2021, 6:06 AM
Hadi
Hadi - avatar
7 Antworten
+ 2
Jazz It is a test example to how access protected member
21st Aug 2021, 6:28 AM
Hadi
Hadi - avatar
+ 1
So for access to "m" i use the public method in otherpkg OR protect pakage?
21st Aug 2021, 6:38 AM
Hadi
Hadi - avatar
+ 1
Ok understand thanks
21st Aug 2021, 5:04 PM
Hadi
Hadi - avatar