How to access the private variables of a class in its subclass?

前端 未结 28 2199
清歌不尽
清歌不尽 2020-12-17 09:46

This is a question I was asked in an interview: I have class A with private members and Class B extends A. I know private members of a class cannot be accessed, but the qu

相关标签:
28条回答
  • 2020-12-17 10:20

    From JLS §8.3. Field Declarations:

    A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class. Nevertheless, a private field is never inherited by a subclass.

    I write the example code:

    public class Outer
    {
        class InnerA
        {
            private String text;
        }
        class InnerB extends InnerA
        {
            public void setText(String text)
            {
                InnerA innerA = this;
                innerA.text = text;
            }
            public String getText()
            {
                return ((InnerA) this).text;
            }
        }
        public static void main(String[] args)
        {
            final InnerB innerB = new Outer().new InnerB();
            innerB.setText("hello world");
            System.out.println(innerB.getText());
        }
    }
    

    The explanation of the accessibility of InnerA.text is here JLS §6.6.1. Determining Accessibility:

    Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

    0 讨论(0)
  • 2020-12-17 10:20
    • Private members cant be accessed in derived class
    • If you want to access means you can use getter and setter methods.
    class A
    {
     private int a;
     void setA(int a)
     {
        this.a=a;
     }
     int getA()
     {
       return a;
     }
    }
    
    Class B extends A
    {
     public static void main(String[] arg)
      {
         B obj= new B();
         obj.setA(10);
         System.out.println("The value of A is:"+obj.getA());
      } 
    }
    
    0 讨论(0)
  • 2020-12-17 10:20

    Below is the example for accessing the private members of superclass in the object of subclass.

    I am using constructors to do the same.

    Below is the superclass Fruit

    public class Fruit {
        private String type;
    
        public Fruit() {
        }
    
        public Fruit(String type) {
            super();
            this.type = type;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
    }
    

    Below is subclass Guava which is inheriting from Fruit

    public class Guava extends Fruit{
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Guava(String name,String type) {
            super(type);
            this.name=name;
        }
    }
    

    Below is the main function where we are creating an object of subclass and also displaying the member of superclass.

    public class Main {
    
        public static void main(String[] args) {
    
            Guava G1=new Guava("kanpuria", "red");
            System.out.println(G1.getName()+" "+G1.getType());
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-17 10:23

    Simple!!!

    public class A{
    private String a;
    private String b;
    //getter and setter are here
    }
    

    public class B extends A{
    public B(String a, String b){ //constructor
    super(a,b)//from here you got access with private variable of class A
    }
    }
    

    thanks

    0 讨论(0)
  • 2020-12-17 10:24

    A private member is accessible in subclass in a way that you cannot change the variable, but you are able to access the variable as read only.

    0 讨论(0)
  • 2020-12-17 10:24

    Obviously, making them protected, or adding setters/getters is the preferred technique. Reflection is a desperation option.

    Just to show off to the interviewer, IF "access" means read access, and IF Class A generates XML or JSON etc., you could serialize A and parse the interesting fields.

    0 讨论(0)
提交回复
热议问题