简体   繁体   中英

How does the subclass object access the private variable of super class,as in java no except the class itself can access the private variable?

This is the pac.java file

package P1; 

public class pac {

    int a;
    public int b;
    private int c;
    protected int d;

    public pac(){
        a=1;
        b=2;
        c=3;
        d=4;
    }

    public void test(){
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

This is other file pack1.java in different package

package P3;

class pac1 extends P1.pac{
    public static void main(String args[]) {
        pac1 ob=new pac1();
        ob.test();
    }
}

Question:
How is that possible that from the two files I have shown, the file P3\\pac1.java creates an object which access the private variable of the class P1\\pac.java ?

The method " test() " is inside of the class "pac". Therefore, when you create an instance of pac, the test method belongs to this instance, and can see and use all the variables.

If you had done this, it would have caused an error.

 int mainC = ob.c; 

the main method (where this is happening) cannot access the variable "c"

What I understand from your unalligned code is that you instantiate a public class and call a public method of it. This is perfectly fine, the test() method has access to variables. You can't access them directly from another method in pac1.

The statement pac1 ob=new pac1(); is creating a new object of Pac1 class and it is possible because it is public .

using ob.test() you are calling a method on the object itself. As private int c is member of the same class in which test() is defined, it can access it.

  • A private member variable or method is accessible throughout the class in which it is defined.
  • You can create the object of Pac1 because the class is public .

To clarify your doubt, you cannot access the private member outside the class like this ob.c in the main method.

Consider a very simple example. For a javabean, we make member as private and getter setters as public . We can access them using only getter and setter as they are public.

public class SomeClass{
   private int someMember;

   public int getSomeMember(){
     return this.someMember;
   }
}

Now from outside the class,suppose in your main method,

SomeClass someClass = new SomeClass();
someClass.someMember; // This is not possible as we are directly accessing private field
someClass.getSomeMember; // possible because `getSomeMember` is public and it belongs to `SomeClass` so can access private member `someMember`.

`

pac1.java only didn't access the private member c of pac.java. It only called the public method test of pac, and that method access the member of the same class.

If I understand you correctly, you are curious how calling ob.test () accesses the private members, despite ob being an object of the derived class. The reason is that whether a method has access is determined at compile time, not run time, and is determined by the location of the method. Your test method is declared in your pac class. So the compiler gives it access to all the private members of the pack class. Now, when it comes to executing the method, it transpires that the type of object your method is executing on is pac1. That does not matter.

This feature of object oriented languages is not a mistake. It makes it possible for the writer of a class to provide limited access to private members, by providing a method that gives only that limited access.


In Java, a subclass object cannot access private variables (or methods) of any super class .
In your example, pac1 class only access public functionality of class pac , such as the public default constructor & the public method test() . Since the implemnetation of these methods is defined within the super class, both can access its private funvtionality - such as the private int c .
If you override these method in subclass however, the overriding methods implementation won't be able to access class pac private functionality.
HTH.

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