Hwee
Hwee

Reputation: 82

Java: inheritance, instance variables and this

I understood that this is a reference to the currently executing object. If that is the case can you explain the behaviour of the following code?

public class Inherit {

    public static class ClassA
    {
        private String privateInstanceVar = "Private";
        public void classAMethod()
        {
            System.out.println("In class: " + this.getClass().getName());
            System.out.println("Can access: " + this.privateInstanceVar);
        }
    }

    public static class ClassB extends ClassA
    {
    }

    public static void main(String[] args)
    {
        ClassB b = new ClassB();
        b.classAMethod();
        //Outputs:
        //In class: Inherit$ClassB
        //Can access: Private

        //System.out.println(b.privateInstanceVar); // Fails to compile
    }   
}

The first line of classAMethod reports that this is a reference to ClassB. However, on the next line I use this to access the private instance variable privateInstanceVar of ClassA, which I shouldn't have access to. (The commented out last line of main shows that this is indeed the case.)

So, my questions are:

  1. If this really is a reference to ClassB, how can I access the private instance variable on ClassA from a method that belongs to ClassB?
  2. Is classAMethod actually a member of ClassA and ClassB at the point of execution?
  3. If the answer to 2. is yes, what are the rules for determining in which context any given line in the method will execute?
  4. If the answer to 2. is no, then what alternative explanation is there for the behaviour of the code?
  5. Is there some bigger picture or subtlety here that I'm failing to appreciate?

Upvotes: 1

Views: 1166

Answers (5)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298978

If this really is a reference to ClassB, how can I access the private instance variable on ClassA from a method that belongs to ClassB?

Because you inherit the method from classA, and that method accesses the private variable.

Is classAMethod actually a member of ClassA and ClassB at the point of execution?

Yes

If the answer to 2. is yes, what are the rules for determining in which context any given line in the method will execute?

Compile-time context: the method or field that is visible from the code you are writing will be chosen. Example:

public static class ClassA{
    private String foo = "bar";
    public String getFoo(){return foo;}
}
public static class ClassB extends ClassA{
    private String foo = "phleem";
}

new ClassB().getFoo() will return "bar", not "phleem", because ClassA does not know about ClassB.

Upvotes: 4

unixorn
unixorn

Reputation: 201

ClassB has access to classAMethod() but it does not have access to privateInstanceVar. So if you try to override classAMethod() or even define some other method which would attempt to access privateInstanceVar you would get an error. The variable would have to be declared protected in order to do that.

Upvotes: 0

Cygnusx1
Cygnusx1

Reputation: 5409

1) Because ClassB inherit from ClassA, so all public, protected method of ClassA are visible to ClassB instance.

2) Yes.

3) unless you override the classAMethod in ClassB, ClassB will just execute the CLassA.classAMethod()

5) This is the normal way inheritance works in Java.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691805

  1. Yes, this refers to the ClassB instance, which is also a ClassA instance. The method classAMethod is defined in ClassA, and thus has access to all the ClassA's private members.
  2. Yes, classAMethod is a method of ClassA, inherited by ClassB
  3. A line executes in the context of the class in which this line is defined.
  4. Not applicable

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76908

I'm not sure what the source of your confusion is. It works exactly as one would expect:

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html

Upvotes: 3

Related Questions