File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how modifier beheave second time ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how modifier beheave second time ..." Watch "how modifier beheave second time ..." New topic
Author

how modifier beheave second time ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
class Default {
private int pri = 5 ;
protected int pro = 6 ;
public int pub = 7 ;
int def = 8;
}

public class Public {
private int pri = 1 ;
protected int pro = 2;
public int pub = 3;
int def = 4;
}


class Test1 extends Default {} // both are in same package
perfectly fine .
Can't inherit private
Can inherit protected - 1] they now will be private ?
Can inherit public - 2] will be public only ?
Can inherit default - 3] they now will be private ?


class Test2 extends Default {} // both are in different package
give an error at compile time .


class Test3 extends Public {} // both are in same package
perfectly fine .
Can't inherit private
Can inherit protected - 4] they now will be private ?
Can inherit public - 5] will be public only ?
Can inherit default - 6] they now will be private ?


class Test4 extends Public {} // both are in different package
perfectly fine .
Can't inherit private
Can inherit protected - 7] they now will be private ?
Can inherit public - 8] will be public only ?
Can't inherit default

first of all sorry for reposting . but i am really confuse in this .
are this 8 points are correct ???

they will be now private , by this line i mean is if now any 3rd class will extends this 2nd class ( Test1 ) then how they beheave ...

please help me ...

thanks a lot .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Access modifiers don't change with "generations" of inheritance. If a member is protected, it will remain protected in any subclasses, as well as in any subclasses of those subclasses, as well as in any subclasses of those subclasses, etc.


[ January 10, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
marc are you sure ?
because it is mention in K&B that protected members will now become pivate after one inheritance ( if both class are in different package ) ...( page 80 )

thanks a lot .
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Hi rathi,
I guess what mentioned in the book relates to the classes in different package. It behaves privately in the subclass residing in a different package.
Thus, Weber example compiles successfully since the classes reside in same package

But check this out:

package superClass;
public class TestProtection{
protected int x = 12;
}



package subclass;
import superClass.TestProtection;

class TestSubClass extends TestProtection {
void method() {
TestSubClass tsc = new TestSubClass();
System.out.println("The value of x is " + tsc.x);
}

}
public class TestSubSubClass extends TestSubClass {
public static void main(String[] args) {
TestSubClass tsc = new TestSubClass();
System.out.println("The value of x is " + tsc.x);
}
}


When u run this example, u get a compilation error in the method TestSubSubClass,
check it out.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

There is the one & only one case in which access level ( access modifier & access specifier ) chage their beheaviour :

two classes are in different package . one has protected member & other is extending it . so now that protected member will be private in this clsss ( sub class ) & can't be access in other class from this class .



please any body tell me atleast that : this above statement is right or not .

thanks a lot .
[ January 11, 2005: Message edited by: rathi ji ]
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
From My example,

public class TestSubSubClass extends TestSubClass {
public static void main(String[] args) {
TestSubClass tsc = new TestSubClass();
System.out.println("The value of x is " + tsc.x);
}
}
The line System.out.println("The value of x is " + tsc.x);
will give an error, did u try compiling, its giving an error here.
saying : The field tsc.x is not visible

What u said is correct,

two classes are in different package . one has protected member & other is extending it . so now that protected member will be private in this clsss ( sub class ) & can't be access in other class from this class .

Thats what i am trying to show through the given example
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
yaa animesh ,
I got the point . you are right .
But it is the only case where access level change beheaviour .

thanks a lot .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If x were truly "private" in TestSubClass, then it wouldn't be visible when inherited by TestSubSubClass. Yet, this works...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
No marc ,
I think this will not work ...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by rathi ji:
... I think this will not work...

Please test this. You'll see that it does work.

Again, if a member is protected, it will remain protected. It does not become "private" with successive inheritance. However, the meaning of "protected" is not so obvious. Section 6.6.2.1 of the Java Language Specifications states...

"Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method [and] ... the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S... "

Ref:
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#62587



In Java Rules, Doug Dunn explores this question in depth, noting that "protected instance variables ... are at once both accessible and inaccessible from subclasses declared in other packages." He further explains that "...the primary use of the protected access modifier is not access control. The primary use of the protected access modifier is to allow subclasses to inherit instance variables and instance methods. The same instance variables and instance methods are generally thought of as being inaccessible outside of the package in which they are declared."

Ref:
http://www.javarules.com/book/sample.pdf (See Sections 2.9 and especially 2.10.1.)

So a protected member remains protected through successive inheritance in that the subclasses continue to (visibly) inherit the member regardless of their respective packages. However, once outside the package in which the member was declared protected, that member is generally inaccessible otherwise.

1/12: EDITED FOR CLARITY.
[ January 12, 2005: Message edited by: marc weber ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how modifier beheave second time ...