Can you declare a private attribute in an abstract class?

Say you have an abstract class:

abstract class PersonAbstract { private $name = "Stack Overflow"; } 

Is it legal to declare an attribute as private in an abstract class? Or the fact that this class needs to be extended, is minimal visibility protected ?

+6
source share
1 answer

Yes, you can have a private field in an abstract class. However, this field will be available only for functions in this abstract class. Any classes that inherit from your abstract class will not be able to access the field.

You can declare both fields and functions public, protected, or private in an abstract class. If a field or function is publicly available, it is available to everyone. If it is protected, it is available only for this class and for any classes that inherit from this class. If it is private, it is available only for this class.

Abstract functions must be implemented by the inheriting class, so it makes no sense (and probably will not work) to have a private abstract function.

+8
source

Source: https://habr.com/ru/post/982553/


All Articles