Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a protected variable from a subclass outside package

Tags:

java

I have gone through so many websites which explains about access specifiers in java such as java papers, java's access specifiers, and many other stackoverflow questions like here.

All these guys explained that the protected member can be accessed by any subclass(also by the subclass out of package) and can be accessed by the package level classes.

While experimenting on protected members, I found out that I'm unable to access a protected member from a subclass outside package.

Check the code below. A public class with protected members:

package com.One;

    public class ProVars {

    protected int i = 900;

    protected void foo()
    {
        System.out.println("foo");
    }

}

Another public class in different package trying to access protected member:

package com.Two;

import com.One.ProVars;

public class AnotherClass extends ProVars {

    public static void main(String[] args) {
        ProVars p = new ProVars();
        System.out.println(p.i);//the field ProVars.i is not visible(Compilation Error)
                p.foo();//the method foo() from the type ProVars is not visible

    }
}

Any explanation is appreciated.

like image 895
chaitanya89 Avatar asked Dec 05 '13 16:12

chaitanya89


People also ask

Can we access protected variable outside the package?

We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.

Can we access protected member outside the package in subclass?

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

How do you access protected variables outside the class in Python?

Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.

Can protected methods be accessed by subclasses?

protected means access to the method is restricted to the same package or by inheritance. So the answer is, yes, protected methods can be overridden by a subclass in any package.


1 Answers

You're attempting to use them as if they were public. They are not public, they are protected.

Example

ProVars p = new ProVars();
p.foo(); // This will throw an error because foo() is not public.

The correct usage, for a subclass to use a protected method or variable is:

public class MyClass extends ProVars
{
     public MyClass()
     {
           System.out.println(i); // I can access it like this.
           foo(); // And this.
     }
}

Why does this work?

Because you've inherited the class. That means you've got all of its methods and it's variables. Now, because your method and variable is protected, it also means that it can be accessed from the subclass. Try and declare them as private and see what happens.

like image 143
christopher Avatar answered Sep 25 '22 12:09

christopher