Reputation: 82
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:
this
really is a reference to ClassB
, how can I access the private instance variable on ClassA
from a method that belongs to ClassB
?classAMethod
actually a member of ClassA
and ClassB
at the point of execution? Upvotes: 1
Views: 1166
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
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
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
Reputation: 691805
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.classAMethod
is a method of ClassA
, inherited by ClassB
Upvotes: 1
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