alalalala
alalalala

Reputation: 889

why get the attributes of the parent class

public class Index2 extends Test {
    public String name = "a";

    public static void main(String[] args) throws Exception {
        new Index2().method();
    }

    public void method() {
        System.out.println(this);
        System.out.println(this.name);
        super.method();
    }

}

class Test {

    public String name = "b";

    public void method() {
        System.out.println(this);
        System.out.println(this.name);
    }
}

console:

com.Index2@15db9742
a
com.Index2@15db9742
b

2 println(this) have same object(com.Index2@15db9742) where not a a? why the object is same(com.Index2@15db9742) but 1st println get "a" and 2nd println get "b"

and why 2nd 'this' is (com.Index2@15db9742) not (com.Test@xxxxxxx)

Upvotes: 0

Views: 80

Answers (2)

Haşim Şemdinoğlu
Haşim Şemdinoğlu

Reputation: 838

When you use the super.method() in child class you get the parent classes (Test class) vars, that does not mean you have an instance of it. You have initiate Index2 so the object name is the same. View the usage of [ Test ttt=new Test(); ] bellow.

public class Index2 extends Test {
public String name = "a";
public static void main(String[] args) throws Exception {
    new Index2().method();
}
Test ttt=new Test();
public void method() {
    System.out.println(this);
    System.out.println(this.name);
    super.method();
    ttt.method();
}}   

To resolve with setter, basically you can use like below

public class Index2 extends Test {

public static String name = "a";

public static void main(String[] args) throws Exception {
    new Index2().method(name);
}

public void method(String name) {
    System.out.println("This is a");
    System.out.println(this);
    System.out.println(this.name);
    super.method(this.name);
}}    
class Test {
public String name = "b";

public void method(String nameVar) {
    this.name=nameVar;
    System.out.println("This is b");
    System.out.println(this);
    System.out.println(this.name);
}}    

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

The short answer is that the subclass doesn't overwrite the attribute name, it shadows it. Test.name still exists, it's just no longer directly accessible from within Index2.

When you call super.method(), the program exits the class Index2 and goes back to Test. Since Index2.name is no longer in scope (no longer shadowing Test.name), test.name is the one that gets used.

It's the same object - as an instance of Index2, the object contains both an Index2.name and a Test.name, the latter of which is only accessible when calling through the superclass method.

If you want to circumvent this behavior - if you want Test.method() to print "a" instead of "b", in this case - then use a getter method. Java has different rules for runtime method resolution than it does for variable name resolution, and will use the version of the method that the subclass implements (in this case Index2), which would return Index2.name.

Upvotes: 1

Related Questions