aspose file tools*
The moose likes Beginning Java and the fly likes protected access modifier using inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected access modifier using inheritance" Watch "protected access modifier using inheritance" New topic
Author

protected access modifier using inheritance

siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
package p1;
public class A
{
protected int a = 10;
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}


package p2;
import p1.A;
public class B extends A
{

public static void main(String[] args)
{
A a1 = new A();
System.out.println(a1.a);
}
}

I am getting a compilation error 'a' has protected access in A. But protected means it can be accessible in sub class which is in different package also right
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

siva chaitanya wrote:But protected means it can be accessible in sub class which is in different package also right

Yes you are right..
Protected members are accessible in sub class even in different package (**But through INHERITANCE only) -> This is very important..
Take a look at this: -
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

siva chaitanya wrote:


Here you are trying to access the protected variable using base class instance through which you can only access public variables.
You need to use either the subclass instance or simply use the variable without using object like:


you can see more examples and at this link.
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
R. Jain wrote:
siva chaitanya wrote:But protected means it can be accessible in sub class which is in different package also right

Yes you are right..
Protected members are accessible in sub class even in different package (**But through INHERITANCE only) -> This is very important..
Take a look at this: -
Thank you for replying me Jain but why cant we create an object of super class and access the protected variable in subclass which is in different package
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

siva chaitanya wrote: but why cant we create an object of super class and access the protected variable in subclass which is in different package

And you're again asking the same question..
Look, there are three cases: -

1). Public members -> Accessible globally through the instance of the corresponding class
2). Private members -> Accessible nowhere else except in the same class
3). Protected Members -> This is a special case. Accessible in the same class or subclass (even in different package)

Now, when it says, accessible in subclass in different package, it means through inheritance.. (And now, you will ask what does this mean ??)

Suppose class A has a protected member protVar. And, we have a subclass B of A.
Now the protVar of class A will be accessible in class A (of course) and in class B (only through the instance class B, but not directly through class A instance)..

So, you can access it in either of the following two ways: -

B obj = new B()
obj.protVar; or
protVar; (notice implicit 'this' keyword)

You can not just access it through the instance of class A, if you are outside class A (no matter where you are)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
RJ has explained it already. You seem to be asking the same question twice. Please explain which part of RJ’s reply you are having difficulty with. Please search this forum for protected access subclasses, and you might find other discussions on the same subject which you find easier to understand. Please have a look at the Java Language Specification, though it may be difficult to read.

Please explain why this first question is so similar to this one, and why the two questions came from the same location.
siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
Thank you jain i got it....
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Campbell Ritchie wrote:
Please explain why this first question is so similar to this one, and why the two questions came from the same location.

You mean this link.. Your link is forwarding to a search page..
But yes these are the two similar questions.. By two different account..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Thank you for correcting the link. I think the similarity of posts will be one of those little mysteries of life.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

siva chaitanya wrote:Thank you jain i got it....


For more details, see the JLS, specifically, this section...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

[EDIT: Oops, sorry. didn't noticed that Campbell already provided this link when I originally posted this]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Think nothing of it. I probably had the link wrong anyway
ven jovovich
Ranch Hand

Joined: Sep 06, 2012
Posts: 33
Hi siva chaitanya,
Please format your code even if it's a one liner. Please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected access modifier using inheritance
 
Similar Threads
Method parameter
Q of inner class in different package
reference of class!
Please explain this code
protected method not being overridden in diff. package