Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

protected doubt  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have come to know that when a subclass outside the package of the superclass acess a protected meber of the super class it becomes private to the sub class. That is no other class in the same package as the sub class can acess it. But will thai apply to a sub class of that particular sub class as well?
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But will thai apply to a sub class of that particular sub class as well?



That subclass wont inherit the member because it is private
 
Sayak Bhattacharyya
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john :-)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If A has a protected method mA() and,
B is a subclass in another package, then a method mB() in B can call mA() in A.

If C is a subclass of B in yet another package, then a method mC() in C can call mA() in A.

Just write some code, it is easy to verify.
[ March 02, 2007: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply... protected variable/method becomes private to all classes in separate package, except the sub-class and its sub-classes in other package.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by John Meyers:


That subclass wont inherit the member because it is private



I wrote the following code, it shows that the protected member of a super class is inherited by the subclass of the subclass in different packages.



The output I got is as below:

In C: protected int a = 10
In D: protected int a = 10
In D: protected int a = 10
In E: protected int a = 10
In C: protected int a = 10
In F: protected int a = 10

The above output certainly says that the protected member
is inherited by all subclasses whether present in same package or not;

So I am interested to know,
where does the protected member access change to private?
Except being unble to access the protected member using an instance of the class in a different package, the access does not seem to be changing to private. (i.e a.a, b.a, etc. not allowed)
 
it's a teeny, tiny, wafer thin ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!