+ 1

What is the difference between public and private method?

19th Dec 2018, 12:43 PM
Lynette Pomasin
Lynette Pomasin - avatar
8 ответов
+ 9
-> A method that is defined public is visible and accessible to all the classes This is useful for when the variable should be accessible by your entire application. Using public with method is the least protective, and should be used only when you absolutely know that you want everything to be allowed access to the methods. -> A method when defined with private keyword helps to encapsulate your methods and variables most effectively. Encapsulation is just opposite to accessibility. Any higher level class cannot be made private.
19th Dec 2018, 1:13 PM
Nova
Nova - avatar
+ 4
public = can be accessed from anywhere private = can only be accessed from the object itself...
19th Dec 2018, 1:00 PM
GiveAway
GiveAway - avatar
+ 3
The difference has been explained, now ill explain why you would want to use either or, so say you have a class called Car and you had just a no parameter constructor, all variables would be supplied when you called a method buildCar(Params) but you also had methods for attaching doors, installing tires, and painting the car. Now you as the writer know the order you want the car to be built. Tires then doors then a paint job and this is how you intend the code to be ran, however you leave all methods as public and publish your code, well now you have complaints, people are painting the frame before installing doors and then putting tires on and your code crashes violently because people dont know how to assemble cars, terrible right, well now imagine if you had the install tires, doors, and apply paint methods private, and that you placed those methods inside of the public build car method, now your users are required to build the car by your design, and you can ensure that it happens the right way.
19th Dec 2018, 2:18 PM
StealthyTaco
StealthyTaco - avatar
+ 3
Continued, this idea is known as the public interface, you restrict what you want your users to do. And you abstract away the minute details of how your cars are built, your users dont need to know that, they just need a car, its like if you were a car salesman and you sold cars to people like a model car would be, some assembly required, how many people do you think would buy your cars? I dont know how to build a car, id go to the next dealer. Anyway, hope this helps a bit.
19th Dec 2018, 2:23 PM
StealthyTaco
StealthyTaco - avatar
+ 1
When u use public that means u want to use that method, variable or ... in other classes of JAVA even they are not in the current package. But in another hand when u use private u want to access to that element only in the current class and in the same package
19th Dec 2018, 1:53 PM
Mohammad Hashemi
Mohammad Hashemi - avatar
+ 1
public = can be accessed from anywhere private = can only be accessed from the object itself...
19th Dec 2018, 4:07 PM
_jason.023
+ 1
idk java but in ruby classes public methods can be accessed anywhere while private methods are only accessible from the class. there is also protected that allows accessing methods etc from objects created from that class. together they allow restricting code in a class to specific areas of your program. this is based on ruby of course but the same concept is in many languages so hopefully this clears things up enough for you to make sense of this chapter of the java course
19th Dec 2018, 8:40 PM
Myersj281
Myersj281 - avatar
0
The public can be summoned by its own direct way, but your own can not be summoned except through the medium
19th Dec 2018, 8:50 PM
karrar eng