aspose file tools*
The moose likes Beginning Java and the fly likes Acess Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Acess Modifiers" Watch "Acess Modifiers" New topic
Author

Acess Modifiers

Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
class A
{
static int i;
private int j;
}

class B extends A {
public static void main(String args[]) {

i = 10;
j = 11;// Error , I Understand this
}
}

What I do not Follow:

class Box {
private double height;

Box(double h) {
height = h;
}
}

class BoxWeight extends Box {
double weight;

BoxWeight(double h, double w) {
super(h);
weight = w;
}
}

height is declared private in Box, so how is it directly accessible using super.
The compiler doesn't give me an error.Why so???
Please Enlighten me folks...


Have A Nice Day !!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

First, I need to make sure you realize that the constructor parameter 'h' in BoxWeight and the member variable 'h' are completely unrelated.

So, anyway, "private" doesn't mean that the outside world can have no effect on a variable. It just means that no outside code can write

Box b = ...
b.h = 37;

No code not in the "Box" class can explicitly mention 'h'. But if Box provides a public method or constructor that itself changes or reads 'h', then any code outside of Box can call that method, and so indirectly access h. The idea is that because 'h'is private, Box has complete control over what happens to 'h'.


[Jess in Action][AskingGoodQuestions]
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
Thank You
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Acess Modifiers
 
Similar Threads
Inheritance, and the mis-use of the 'static' keyword
cannot find symbol - constructor error while extending a class
(Inheritance) please Help!
converting into applet
End of file while parsing