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 Spring in Action this week in the Spring 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: 24187
    
  34

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Acess Modifiers