Private members in Java inheritance

后端 未结 5 658
执笔经年
执笔经年 2020-12-01 06:43

I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.

C

5条回答
  •  一向
    一向 (楼主)
    2020-12-01 07:26

    You will be satisfied here 100%. I tested it on my computer and what I concluded I'm going to post it here. Just go through the program written below, see the program output and READ THE CONCLUSION given at the end. To test it yourself, copy the whole program and save it in a file named "InheritanceTest.java" then compile it and finally run it.

    Program

    // Testing if a subclass can access the private members of a superclass
    
    class Class1 {
        private String name;
    
        public void setName(String name) {
            this.name = name;
            System.out.println("The name has been set successfully.");
        }
    
        public void showName() {
            System.out.println("The name is: " + name);
        }
    }
    
    class Class2 extends Class1 {
        private int age;
    
        public void setAge(int age) {
            this.age = age;
            System.out.println("The age has been set successfully.");
        }
    
        public void showAge() {
            System.out.println("The age is: " + age);
        }
    
        public void displayName() {
            //Accessing the private member of superclass here
            //System.out.println("The name is: " + name); //error, can't compile because access to the private member name of the superclass Class1 is not permitted here.
        }
    }
    
    class InheritanceTest {
        public static void main(String[] args) {
    
            Class1 c1 = new Class1();
            Class2 c2 = new Class2();
    
            c1.setName("Name_C1");
            c2.setName("Name_C2"); //No error, setName() is a public member of the superclass which indirectly gives access to the private member "name".
    
            c1.showName();
            c2.showName(); //No error, showName() is a public member of the superclass which indirectly gives access to the private member "name".
    
            c2.setAge(25);
            c2.showAge();
    
            //c2.displayName(); //error
        }
    }
    

    Output

    The name has been set successfully.
    The name has been set successfully.
    The name is: Name_C1
    The name is: Name_C2
    The age has been set successfully.
    The age is: 25
    

    Conclusion

    Yes, a subclass can indirectly access the private members of a superclass. A subclass can't directly access the private members of a superclass.

    All the public, private and protected members (i.e. all the fields and methods) of a superclass are inherited by a subclass but the subclass can directly access only the public and protected members of the superclass. If an inherited member from a superclass gives access to a private member of the superclass then the subclass can use this inherited member to access the private member of the superclass.

提交回复
热议问题