What is the difference between public, private, and protected?

后端 未结 17 1587
抹茶落季
抹茶落季 2020-11-21 07:54

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them?<

17条回答
  •  后悔当初
    2020-11-21 08:10

    The difference is as follows:

    Public :: A public variable or method can be accessed directly by any user of the class.

    Protected :: A protected variable or method cannot be accessed by users of the class but can be accessed inside a subclass that inherits from the class.

    Private :: A private variable or method can only be accessed internally from the class in which it is defined.This means that a private variable or method cannot be called from a child that extends the class.

提交回复
热议问题