Search code examples
javaprivatepublic

Need understanding for public and private


I have pasted the code, now my query is that in 1st class named "accesssp" I have added object and then written SOP then why cant I have output as value of B ?? b's access is private but I am fetching value in same class

I've done section BOLD which I don't understand.

class accesssp  {

public int a=56;
private int b=5566;
public int c=58766;

System.out.println(b);

}

class accesssp1 extends accesssp{

public void accessd()   {

    System.out.println(a);
    System.out.println(c);

}

}
public class Access_Spf {

public static void main(String[] args) {


    accesssp1 sp1 = new accesssp1();
    sp1.accessd();
}

}

Solution

  • System.out.println(b); is neither in the main() method nor in any function. This cannot be executed directly as you have done. On a side note, variable b will not be accessible in accesssp1 class.