0

My Base class in one package

package com.practice.array;

public class Employee { 
    private int age=1;
    protected String name="string";
    public String lastname="string1";
    String familyName="string2";    
}

and child class in other package

package com.practice.binarytree;

import com.practice.array.Employee;

public class Demo extends Employee{
    public static void main(String[] args) {
        Employee emp = new Employee();
        System.out.println(emp.name);//getting compile time error here 
    }
}

Here I am getting compile time error as change visibility of 'name' to 'protected' but name variable is already declared as 'protected'

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    "Here I am getting compile time error as change visibility of 'name' to 'protected'" - I very much doubt that. Please post the *exact* error message. Note that you *shouldn't* have access to the `protected` variable here. See http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2 – Jon Skeet Aug 13 '17 at 08:06
  • You can only invoke the protected variable through Child reference. So you need to invoke it on Demo object. – Sid Aug 13 '17 at 08:06
  • (If you'd used `Demo demo = new Demo();` then you'd be able to print out `demo.name`.) – Jon Skeet Aug 13 '17 at 08:06

0 Answers0