In Java, difference between default, public, protected, and private In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance? Answer: The official tutorial may be of some use to you. | Class | Package | Subclass | Subclass | World | | |(same pkg)|(diff pkg)| ————————————+———————+—————————+——————————+——————————+———————— public | + | + | + | + | + ————————————+———————+—————————+——————————+——————————+———————— protected | + | + | + | + | o ————————————+———————+—————————+——————————+——————————+————————
undefined
What is the difference between public, private, and protected? When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them? Examples: // Public public $variable; public function doSomething() { // ... } // Private private $variable; private function doSomething() { // ... } // Protected protected $variable; protected function doSomething() { // ... } Solution : You use: public scope to make that variable/function available from anywhere, other classes and instances of the object. private scope when you