Menu
Topics Index
...
`

IndependentClass - Java Example Program


IndependentClass
package packageTwo;

class IndependentClass
{
    public static void main(String[] args)
    {
        packageOne.Protection p = new packageOne.Protection(); // LINE A
        //System.out.println("value of i :" + p.i);
        //System.out.println("value of pri_i :" + p.pri_i);
        //System.out.println("value of pro_i :" + p.pro_i); // LINE B
        //protected cannot be accessed in different package no-subclass
        System.out.println("value of pub_i :" + p.pub_i);
    }

}
OUTPUT

value of pub_i :20

DESCRIPTION

In the above program we have created an object for packageOne.Protection with the reference p. At LINE B p.pro_i is not accessible because IndependentClass is not a subclass and also it is under different package.

THINGS TO TRY
  • Uncomment LINE B to see compilation error as p.pro_i is not accessible.

© meritcampus 2019

All Rights Reserved.

Open In App