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

后端 未结 17 1870
抹茶落季
抹茶落季 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:25

    Public: is a default state when you declare a variable or method, can be accessed by anything directly to the object.

    Protected: Can be accessed only within the object and subclasses.

    Private: Can be referenced only within the object, not subclasses.

提交回复
热议问题