简体   繁体   中英

Parent member function hiding child's data member

I've a code:

class Parent{
    int x=10;
    void show(){
        System.out.println(x);
    }
}

class Child extends Parent{
    int x=20;
    public static void main(String[] args){
        Child c=new Child();
        c.show();
    }
}

Here when I run this program the output comes is 10. Which means at runtime Parent member-function not using the Child data-member with same name(data hiding). The thing I know is that whenever we are extending a class its parent class member-function and data-members are available to Child class, then when I say c.show() why it takes the data-member of Parent class not Child class. Also, I want to know that when we create an object of Child class its Parent class data-members are put in the Parent section of Child class object in Heap, but what happens to member-functions?

The thing I know is that whenever we are extending a class its parent class member-function and data-members are available to Child class, then when I say c.show() why it takes the data-member of Parent class not Child class.

Your understanding correct. But the otherwise is not true. Parent class sub-object doesn't have access to child class sub-object. Since show() is in the scope of base class Parent , it is accessing it's own members. Try to comment the member x in the parent class and see if your program compiles.

Commented x in parent class and see the output yourself

The child can't hide a field from a method in the parent class by simply declaring its own version of the field. You are applying the idea of "data hiding" in reverse. The child can never hide fields from the parent, but the parent can hide fields from the child (by declaring x as private ). If you wanted to, you could use encapsulation to achieve your desired effect , like:

class Parent{
    private int x=10;

    public int getX() {
        return x;
    }

    void show(){
        System.out.println(this.getX());
    }
}

class Child extends Parent{
    int x=20;

    @Override
    public int getX() {
        return x;
    }

    public static void main(String[] args){
        Child c=new Child();
        c.show();
    }
}

By overriding the getX() accessor method, the child can hide the parent's value of x from the rest of the world.

Some other points worth discussing:

The thing I know is that whenever we are extending a class its parent class member-function and data-members are available to Child class

This is not entirely correct. Private methods and data members will not be (directly) available to the child class. Only public, protected, and "package-level" methods and fields will be directly accessible to the child class.

Also, I want to know that when we create an object of Child class its Parent class data-members are put in the Parent section of Child class object in Heap, but what happens to member-functions?

Member functions aren't created per-instance. It would be terribly inefficient if the JVM worked like that. Instead, the method implementations for a given class are defined only once, as part of the Permanent Generation which holds things like class definitions and other internal JVM state. All instances of a given class will share the same method definitions.

In the case of a child class, it will share the method definitions of its parent, and it will also have its own definitions added for any methods that exist just in the child (and for any methods that it overrides).

X in child is only shadowing over the base class, not overriding it. The base class member variable is not altered, only hidden by a separate variable in the child class that happens to have the same name. They.are still completely different variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM