[Solved] private member cannot accessible in child class but my below code give successful run why [closed]


In your code you are setting name as private String :

 private String Name;

and accessing it using a public function getName() and returning its value to the caller:

 public String  getName()
    {
        return Name;
    }

and as your (getter) function getName() has a public access modifier you can
call it using child class object:

 childClass c=new childClass();
    c.display("Hazrat Ali");
    System.out.println("My father name is:"+c.getName());

Encapsulation / Data hiding

To achieve encapsulation in Java −

  1. Declare the variables of a class as private.

  2. Provide public setter and getter methods to modify and view the variables
    values.

Benefits of Encapsulation

  • The fields of a class can be made read-only or write-only.

  • A class can have total control over what is stored in its fields.

    Encapsulation provide control over the data.

solved private member cannot accessible in child class but my below code give successful run why [closed]