Why can we reduce visibility of a property in extended class?
field, inheritance, java, visibility
Solution
This is because `Parent.a` and `Child.a` are different things. `Child#method()` `@Override`s `Parent#method()`, as they are methods. Inheritance does not apply to fields.
From the Oracle JavaTM Tutorials - Inheritance, it was written that:
What You Can Do in a Subclass
- The inherited fields can be used directly, just like any other fields.
- You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
- You can declare new fields in the subclass that are not in the superclass.
Problem
I have two classes, `Parent`: ``` public class Parent { public String a = "asd"; public void method() { } } ``` And `Child`: ``` public class Child extends Parent{ private String a = "12"; private void method() { } } ``` In `Child`, I try to override the parent `method` which gives a compile time error of `cannot reduce visibility of a method` which is fine. But, why is this error not applicable to property `a`? I am also reducing visibility of `a`, but it doesn't give an error.