1

I'm obviously missing the obvious in this but given:

package a;
public class Class1 {
    protected int a=1;
}

package b;
import a.*;

public class Class2 extends Class1 {
    Class2() {
        Class1 c1=new Class1();
        Class2 c2=new Class2();
        System.out.println(a);     //1
        System.out.println(c1.a);  //2
        System.out.println(c2.a);  //3
    }
}

I know //1 is fine due to being used through inheritance and //2 fails because it's not being access through inheritance, but why is //3 ok too? I thought variable a was being accessed through a new object and a resides in Class1?

Thanks.

2
  • not relevant to the actual question, but calling this constructor will cause a StackOverflowError. Commented Feb 18, 2013 at 20:19
  • Yes, thanks. I put it in main but then took it out to save on the number of lines of code and to allow me to put in line //1 :) Commented Feb 18, 2013 at 20:24

4 Answers 4

4

When you are manipulating an object inside its class, you have full access to all his attributes, including the private ones. As c2 is a instance of Class2 and you are manipulating it inside the Class2 code, you can see the protected attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. So assuming no data hiding Class2 inherits all of Class1 so 'a' becomes part of Class2 as a protected member effectively.
including the private ones. -> Sorry??
@RohitJain If you have a class called Test and you have a attribute declared private int abc; you can declare a variable Test t = new Test(); and do something like t.abc = 123; if you are inside the class Test
@DanielPereira. You don't mean - from anywhere, do you? And if not, then your answer is not very clear about that.
4

Since any object of Class2 is-a Class1, it can access all of Class 1's member variables with scopes default, protected and public.

Besides, trying to understand scope and inheritance rules through working with objects that are of the class you are using to play with scopes/inhertiance is not a good idea, since it works in a different way than through a third-party (which is the most common use.)

For instance, this is allowed:

public class Something {
   private int something;

   public int stealSomething(final Something otherthing) {
       return otherthing.something;
   }
} 

Try making a third class, that is not in the hierarchy of the classes you are using to test.

1 Comment

Cheers. I think this stems from normally using base classes as the reference type when creating subclasses and observing the error.
3

why is //3 ok too?

And why shouldn't it be ok? Given that Class2 is the subclass of Class1, so a protected fields of Class1 are accessible through the instance of Class2. And that is what you are doing here. c2 is an instance of Class2, and a field is visible for it.

NOTE: A protected member is accessible to any direct subclass, whether in same package or in different package.

I thought variable a was being accessed through a new object

Yeah, that's true.

and a resides in Class1?

That really doesn't matter here. As far as a is accessible to Class2 instance, it is valid.

And just FYI, your code will die of StackOverflowError. You need to take care of it.

2 Comments

Yes, thanks. I put it in main but then took it out to save on the number of lines of code and to allow me to put in line //1 :) this is just testing code not a real bit of code
@Neil. Ok. Then it's fine.
1

I don't understand the problem.

Class1 has a variable named 'a'. Since it is 'protected', that variable is visible within any object of Class1 and any object of a class extending Class1.

If 'a' were private, then it would not be visible this way in objects of classes extending Class1.

1 Comment

"Class2 extends Class1" - it IS a subclass, different package does not matter here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.