开发者

Something weird is happening to the Person

In the following java code

public class Person {
    int age = 18;
}

class Student extends Person {
    public Student() {
        thi开发者_开发技巧s.age = 22;
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.doSomthing();
    }

    void doSomthing() {
        System.out.println(this.age);
        System.out.println(super.age);// Here is something weird, at least for me till rightNow()
    }
}  

Why the super.age value is 22 , the same value as the sub-class's age value, Isn't it supposed to be 18;

Any help is appreciated.

Thanks in advance.


Age is a field in the super class. In the constructor of the subclass, when you say this.age = 22, you are updating the instance variable in the super class.

Try the following ... I dont have a compiler handy but i think it might do what you are expecting.

public class Person {
    int age = 18;
}

class Student extends Person {

    int age; // Hides the super variable

    public Student() {
        this.age = 22;
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.doSomthing();
    }

    void doSomthing() {
        System.out.println(this.age);
        System.out.println(super.age);
    }
}  


this is behaving as you would expect. You haven't declared an 'age' member of Student, so this.age naturally references 'age' defined in the superclass.

The code below will provide the behaviour you are expecting (although shadowing variables like that is often a very bad idea).

public static class Person {
    int age = 18;
}

public static class Student extends Person {
    int age = 18;

    public Student() {
        this.age = 22;
    }

    void doSomthing() {
        System.out.println(this.age);
        System.out.println(super.age);
    }
}


No, that is correct. In the constructor, you are overriding the super class's age. You could instead do something like this:

public class Person {
    public int getAge() {
        return 18;
    }
}

class Student extends Person {
    public Student() {
    }

    @Override
    public int getAge() {
        return 22;
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.doSomthing();
    }

    void doSomthing() {
        System.out.println(this.getAge()); //22
        System.out.println(super.getAge()); //18
    }
}  


Student inherits age from parent, so there is no difference between age and super.age


No, what is happening is correct. When you create a subclass (Student is a subclass of Person), that subclass inherits all of the fields (variables) from the superclass. However, there is only one set of variables: there is only one value for age, even though it is inherited. In other words, when a class inherits a field, it doesn't create a new copy of it - there is only one copy per student.


In this source, this and super are the same instance variable because you define it in the super class an inherited in the subclass.

When you create your Student you initilize it to 22 and that's it.


Nothing strange, it's behaving correctly. Class Student doesn't have a private variable age, which would overwrite parents variable.


You're setting age in your Student class, but the parent is the one declaring age and they share the same variable - therefore, it makes sense that the value was modified. Overriden methods would be different, however.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜