Are the private variables and private methods of a parent class inherited by the child class in Java?

Java 8Object Oriented ProgrammingProgramming

No, a child class can’t inherit private members of the parent class, it can inherit only protected, default, and public members of it. If you try it gives you a compile time error as: −


class Super{
   private int data = 30;
   public void display(){
      System.out.println("Hello this is the method of the super class");
public class Sub extends Super{
   public void greet(){
      System.out.println("Hello this is the method of the sub class");
   public static void main(String args[]){
      Sub obj = new Sub();

On executing this example it will give an compiletime error as shown below −

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
   The field is not visible
   at Sub.main(
Published on 05-Jan-2018 17:17:07