Forums Register Login

Private members accessible through inheritance

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.....
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Thanks Mr. white I have the concept clear.
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1293 times.
Similar Threads
Protected and default access of a class member
Inheritance of all members
hiding private method in subclass
Access Modifiers little bit confusion... Help with World..
Which came first constructor or object ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:22:01.