• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected Access Confusion

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 3 files A.java, B.java and C.java all the 3 java files are in the same directory.

A.java -



B.java -



C.java -




Compiled all the 3 files as below -



Now I have 2 class files A.class and B.class package abc and another set of 2 files C.class and D.class in packages xyz.

I excepted class D will throw an error(but it didn't) when it tries to invoke protected go() method of class A as D is in a separate package and it doesn't inherit the go() method directly from A, rather it does it through C.

To my understanding a protected method when inherited from a different package will get inherited as private in C. Then how could D inherit a private member from C and calls it.

Please help me in understand this.

Thanks in Advance.
-Jerry.

(No shouting in topic titles, please)
[ February 26, 2007: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To my understanding a protected method when inherited from a different package will get inherited as private in C. Then how could D inherit a private member from C and calls it.


the protected method will be private to C.but,it can be accessed by subclasses of C.since d is subclass of C,no problem.
But it cannot be accessed by other classes in the package "xyz".
[ February 26, 2007: Message edited by: Raji Rama ]
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raji Rama:

the protected method will be private to C.but,it can be accessed by subclasses of C.since d is subclass of C,no problem.
But it cannot be accessed by other classes in the package "xyz".

[ February 26, 2007: Message edited by: Raji Rama ]




I am afraid Raji, I don't understand how a method which is private to a class can be accessed by its subclass. The private method of the super class will not be even visible to subclass.

-Jerry
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As K&B puts it "Once the subclass outside the package inherits the protected member, that member becomes private to any code outside the subclass, with the exception of subclasses of the subclass"
[ February 27, 2007: Message edited by: Bunty Naagar ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We must remember the factors behind protected access:

1-You make a class member variable and methods protected if you want to make it accessible outside the package too BUT only through inheritance. Protected means package + kids. Kids are the subclasses that may be outside the package of the class in which the protected member is defined.
2-Outside the package, you can�t access protected member of a class using the reference of the class in which it is defined. This statement needs good clarification. OK! So let�s give you a good example to get insight of the concept

package certification;

public class A {
protected int x=10;
}


package scjp;
import certification.A;

public class B extends A{

void display(){
//First way to access protected member x of class A in package certification
System.out.println(�x = � + x);
//Second way to access protected member x of class A in package certification
B b1 = new B();
System.out.println(�x = � + b1.x);

//and what can�t be done, means restricted
A a1 = new A();
System.out.println(�x = � + A1.x);//compiler error

}
}


/* MORE CLARIFICATION*/
package scjp;

public class C extends B {
void display() {
//First way to access the protected member of class B
System.out.println(�x = � + x);
//Second way to access the protected member of class B
C c1 = new C();
System.out.println(�x = � + c1.x);

//What causes compilation error, or restricted to do
B b1 = new B();
System.out.println(�x = � + b1.x); //NO, compiler error
A a1 = new A();
System.out.println(�x = � + a1.x);//NO, compiler error
}
}


Conclusion: Protected member of a class in other package can only be accessed by the subclass in another package directly as if it would member of the same class; in other words without using dot �.�operator, or using the reference of self, that extends the parent class. You see that, class C can�t acccess the member x using reference of the class B but inside class B the protected member x of class A of package certification is easily accessed. It directly means that for the extending class the protected member remains protected for the next inheriting classes but private for others which are not subclasses. Here private for others means, if there is another class in package �scjp� that is standalone means does not extend the class B or so. If it tries to access the protected (inherited from A) member x, it fails to access because it is private to outside world except the inheriting classes.


[ February 28, 2007: Message edited by: Chandra Bhatt ]
[ February 28, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra..Thanks for the pretty descriptive explanation..!!
But, to me -- there is a correction thr....
In package SCJP..
instead of class B-- should it not be Class B extends class A..
I think ..only when class B extends A, your explanation will hold true..
Please clarify..
Thanks.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic