jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Acess Modifiers" Watch "Acess Modifiers" New topic

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) {
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

Joined: Jul 08, 2003
Posts: 24199


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
It's not a secret anymore!