• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

how modifier beheave second time ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x were truly "private" in TestSubClass, then it wouldn't be visible when inherited by TestSubSubClass. Yet, this works...

 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No marc ,
I think this will not work ...
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic