wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Private members accessible  through inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private members accessible  through inheritance" Watch "Private members accessible  through inheritance" New topic
Author

Private members accessible through inheritance

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Please consider the following code:
class Base {
private int i;
private String s;
protected int j;
Base(int j) {
i = 45;
s = "hello";
this.j = j;
}
protected void print() {
System.out.println("i :"+i+" s: "+s+" j :"+j);
}
}
class Derive extends Base {
String s1 = "shekar";
Derive() {
super(100);
}
public static void main(String[] args) {
Derive obj = new Derive();
obj.print();
}
}
Normally private members of a class cannot be accessed. Even in the above code the private members are not accessible. But i am still able to view the contents of the private variables and when I try to access the variable 'i' and 's' unknowingly it can give me an error. Is it that only the programmer has to be careful in initializing the private members separately and protected members separately and also while printing the members of the class should we do it separately or is there any restriction in java to do this?


Thanks,
Shekar
Thomas White
Ranch Hand

Joined: Feb 01, 2003
Posts: 32
Chandra,
Your derived class can call the print() method of the base class and thus the values of i and s are printed to the console, but that does not violate the concept of private variables. The derived class cannot access i and j directly and thus cannot change them. You want to make fields private when you don't want another class to have direct access to them (and therefore possibly changing them in unwanted ways), however, as often is the case, the protected print() method in your base class allows subclasses and classes of the same package to use i and j (in this case print their values to standard output) without modifying them.
Also,in case its not clear, because int j is protected, classes of the same package as the base class and subclasses outside of its package can access and modify j directly.
[ September 12, 2003: Message edited by: Thomas White ]
[ September 12, 2003: Message edited by: Thomas White ]
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Thanks Mr.White for the reply but do you have any idea about the libraries which have been built in such a way that the private members are visible in such a way (as is the case above where 'i' and 's' are visible to the end user, if at all). I suppose the above code is not cleaner and i still have my doubts about initializing the variables separately for private and protected. can you help me regarding this.
Thanks once again.
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
You WILL NEVER GET THE VALUES OF i,s and j......But if you want the same values in other class......do this.
Deaclare.....
i1 , s1 , j1...Then assign as these follows
i1=1
s1=s
j1=j

Then get the values of i1,s1, and j1. hope this will solve your confusion.....


Thanks,
Mathews
Thomas White
Ranch Hand

Joined: Feb 01, 2003
Posts: 32
Chandra,
Its ok for the end user to see the values of i,s and j. Thats not a violation of the concept of private and protected variables. The idea is that you declare variables private if you do not want a class other than the class they reside in to be able to access and manipulate them. You declare a variable protected if you only want classes of the same package or subclasses of that particular class to access you variable, but not classes that are outside the package and not subclasses. Since your
print() method is protected, it allows package level classes and subclasses of your base class to call it, and thus the private variables i and s(along with j) are printed, but the variables are still private. Class Derive cannot directly access them. For example you cannot say
"super.i = 54;" in Derived because i is private, you also cannot say in Derived "System.out.println(super.i);". But you can use your print() method to print the values because the method has protected access.
Now as far as variable j is concerned, Derived can access this variable directly because it is protected, so you could say "super.j = 101;" for example to change the variable(*note: you can't however do this in you main method of Derive because super.j is not a static variable).
As far as your question about declaring variables separately, I'm not sure what you mean but the way you have them
"....
private int i;
private String s;
protected int j;
..."
is fine.
Thomas
[ September 12, 2003: Message edited by: Thomas White ]
[ September 12, 2003: Message edited by: Thomas White ]
[ September 12, 2003: Message edited by: Thomas White ]
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Thanks Mr. white I have the concept clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private members accessible through inheritance
 
Similar Threads
Protected and default access of a class member
Which came first constructor or object ?
Access Modifiers little bit confusion... Help with World..
hiding private method in subclass
Inheritance of all members