“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Public methods and variables can be used by any other class without having to create an instance of that class eg: (Toy.java) and (Fun.java) are two public classes. Toy.java has a method car() in it. In order for Fun.java to use it it should do the following to invoke the method Toy.car()
Private methods and variables of a class (Toy.java) can only be used by another class (Fun.java) by creating an instance of that class. eg: Toy.java has a method car() in it. In order For Fun.java to use it it should do the following to invoke the method Tou toy = new Toy() toy.car()
A Protected variable or method can only be accessed by the classes that extend it (ie by using inheritance)
Unfortunately I believe that buturab is discussing Static vs. non-Static methods and variables. In the case of Public, Private and Protected, that is used to describe which programs can access that class or method:
Public – any other class from any package can instantiate and execute the classes and methods Protected – only subclasses and classes inside of the package can access the classes and methods Private – the original class is the only class allowed to executed the methods.
Public methods and variables can be used by any other class without having to create an instance of that class
eg:
(Toy.java) and (Fun.java) are two public classes. Toy.java has a method car() in it. In order for Fun.java to use it it should do the following to invoke the method
Toy.car()
Private methods and variables of a class (Toy.java) can only be used by another class (Fun.java) by creating an instance of that class.
eg: Toy.java has a method car() in it. In order For Fun.java to use it it should do the following to invoke the method
Tou toy = new Toy()
toy.car()
A Protected variable or method can only be accessed by the classes that extend it (ie by using inheritance)
Unfortunately I believe that buturab is discussing Static vs. non-Static methods and variables. In the case of Public, Private and Protected, that is used to describe which programs can access that class or method:
Public – any other class from any package can instantiate and execute the classes and methods
Protected – only subclasses and classes inside of the package can access the classes and methods
Private – the original class is the only class allowed to executed the methods.
And in case if there is no modifiers specified, it means, only the classes
inside the package can access this class and its methods
To add to Radha’s comment, default access specifier is also called “Friendly”
Manta is absolutely right !
I have double checked the answers from the book.
Good work Manta !!!!
buturab, one correction on protected methods: they may also be accessed by other classes in the same package.
Leave an Answer/Comment