Reputation: 19
public class Test {
public static void main(String[] args) {
B b = new B();
b.test();
}
}
class A {
String aString = "a";
void test() {
System.out.println(aString);
}
}
class B extends A {
String aString = "b";
}
why the cant input out "b"?
i just think b.test()
will call method of superClass, so b will user superClass's attribute.
Upvotes: 0
Views: 1110
Reputation: 13225
class A { String aString = "a"; void test() { System.out.println(aString); } } class B extends A { String aString = "b"; }
Creates 2 aString
variables for B. While A
will not know about B
extending it, B
can always access "things" from its parent class A
, using the super
keyword. So when you are in B
, you can use aString
and super.aString
and they will refer the 2 different variables:
class Ideone {
public static void main (String[] args) {
A a=new A();
B b=new B();
a.test();
b.test();
b.suptest();
}
static class A {
String aString="A.aString set from A";
void test() {
System.out.println("A.test(): "+aString);
}
}
static class B extends A {
String aString="B.aString set from B";
{
super.aString="A.aString set from B";
}
void test() {
System.out.println("B.test(): "+aString);
}
void suptest() {
System.out.print("B.suptest() calling super.test(): ");
super.test();
}
}
}
You can try it on IdeOne, produces output
A.test(): A.aString set from A B.test(): B.aString set from B B.suptest() calling super.test(): A.test(): A.aString set from B
Where the first two lines show nothing fancy, A.aString
and B.aString
both contain their initial value.
But the third output line shows that super.test()
call in B
really "ends" in A.test()
, and that the initializer block in B
really altered the inherited aString
field.
(Side note: the static class
magic relates to the example being contained in a single file, it doesn't affect the inheritance-part)
Upvotes: 1
Reputation: 35106
The variable in b is hiding the variable in a. You can fix it by removing the type in b
class B extends A {
public B () {
aString = "b";
}
}
Upvotes: 1
Reputation: 1
You are correct.
Since Class A
and the method test()
are not abstract and the fact that the method test()
was not overrided by its subClass B
, the return will be exactly how was specified in Class A
even if you call it from a B
instance.
Upvotes: 0
Reputation: 181734
why the cant input out "b"? i just think
b.test()
will call method of superClass, so b will user superClass's attribute.
Java class and instance variables can be inherited, but they are not virtual. That is, all variable identifiers appearing in Java code are resolved at compile time -- there is no dynamic resolution for them.
Thus, the appearance of identifier aString
in method A.test()
is resolved at compile time to the aString
attribute of class A
. This is the attribute that all invocations of that method will access, regardless of the actual class of the object on which it is invoked. If you want to use the attribute of class B when test()
is invoked on an instance of that class then provide getter methods in classes A
and B
and have A.test()
obtain the string via those.
Upvotes: 4