Search code examples
javaoopencapsulation

Create an attribute without encapsulation


Simple question about oop.

Let's pretend I have a class with an attribute in it.

public class Person {
  // Attributes
  private int age; 
}

If I remove the encapsulation of my attribute age, will it still be private? or something else?

Thanks


Solution

  • If you do it, the attribute will be accessible from the class and from the package as well, but it won't be accessible from subclasses and the rest of the project. Therefore, the attribute will be some kind of semi protected variable. It is not a recommended approach in OOP.

    You might find this tutorial helpful.

    Hope it helps.

    Clemencio Morales Lucas.