• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected access modifier using inheritance

 
siva chaitanya
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you jain i got it....
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for correcting the link. I think the similarity of posts will be one of those little mysteries of life.
 
Henry Wong
author
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think nothing of it. I probably had the link wrong anyway
 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi siva chaitanya,
Please format your code even if it's a one liner. Please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic