aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected variable access Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "protected variable access" Watch "protected variable access" New topic
Author

protected variable access

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 380


Look a code :



Following class tries to access the protected variable through the Bed class.



points of my logic :
Bed class is visible in UserBed.
A protected variable can be accessed in different package by a subclass.

then why i cannot access protected variable ins through the object or reference of Bed class in UserBed class.




Regards
naveen
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

From JLS:

6.6.2.1 Access to a protected Member
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

For more detail : http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2
Amu Hlongwane
Greenhorn

Joined: Dec 13, 2007
Posts: 14
The moment you say new Super class inside the Child class you defeating the benefits of inheritance, which 1 of them is to be able to access Super class variables and methods including those marked protected. I'd advise you to use this and super, or call directly without using a reference varible.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Protected does not mean that any subclass instance can access the protected member of any superclass instance. It means that a subclass instance is allowed access to the protected member contained in itself or another subclass instance.

So you are allowed to do this:

And you are allowed to do this:

But NOT this:
Amu Hlongwane
Greenhorn

Joined: Dec 13, 2007
Posts: 14
But Protected DOES mean that any subclass instance can access the protected member of any super class.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1121
Dennis Deems wrote:Protected does not mean that any subclass instance can access the protected member of any superclass instance. It means that a subclass instance is allowed access to the protected member contained in itself or another subclass instance.



Right, makes perfect sense. So, the following works just fine -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1121
Ok, this explains it perfectly -

Seetharaman Venkatasamy wrote:From JLS:

Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C.


Regards,
Dan
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 276

Indeed, Plus one


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: protected variable access
 
Similar Threads
confuse about protected and static
protected or not protected.
default vs protected access
Protected Trouble
mock test question on protected access specifier