File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in  access modifier. Please explain this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in  access modifier. Please explain this code" Watch "doubt in  access modifier. Please explain this code" New topic
Author

doubt in access modifier. Please explain this code

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
package certification;
public class Parent{
protected int x=9;
}

package other;
import certification.Parent;
class Child extends Parent {
public void testIt(){
Parent p=new Parent();
System.out.println("X in parent is" + p.x);
}
}

In the above mentioned code since the child class is in different package and creating a instance of superclass and accessing its protected member from an instance its a compiler error but in the below mentioned code if it accessed in the same way in the same package it works. Can anybody explain why it happens like this. Why it works in the same package but not in the different package.

"Code in the same package"

package certification;
public class Parent{
protected int x=9;
}

class Child extends Parent {
public void testIt(){
Parent p=new Parent();
System.out.println("X in parent is" + p.x);
}
}

Thanks All
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Nikhil,




1- A member variable marked protected can only be accessed in the same package. And it can be accessed by the subclass (in the different package) without dot (.) operator after Parent class reference variable.
You are trying to access the protected member of the Parent class in the Child class that is in the different package using the Parent class reference variable hence compiler error.

See the code below, how you can access x of the Parent class




Didn't you come back to this post
doubt in static method

Regards,
cmbhatt
[ April 29, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
I have one doubt when it is accessed with "."(dot) operator in the differnt package in a subclass it gives compiler error but if it is accessed in the same package in a subclass it does not give a compiler erroe. Why?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
I have one doubt when it is accessed with "."(dot) operator in the differnt package in a subclass it gives compiler error but if it is accessed in the same package in a subclass it does not give a compiler erroe. Why?



That is what protected access means!

You want to allow other packages subclass to see your protected members through inheritance only, not through reference variable of that class.

Until the member variable is private, classes, subclass of the same package can access that using "." operator, no issue.



cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks chandra bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in access modifier. Please explain this code