ANSWERS: 4
  • In Java and many other Object Oriented languages declaring a data member or method private means it can only be accessed inside of the class, where as public means anyone can access it. In Java there is another distinction, protected. If something is protected it can only be accessed by the class that it is contained in and any class that inherits that class. It is generally considered good practice to declare most all data members to be private so that someone else doesn't change one and cause side effects to your code. In this case if someone needs access to a private or protected variable then you write simple setter and/or getter methods. Example: public class stuff { private int x; . . . . . //simple getter public int getX() { return x; } //simple setter public void setX(int a) { x = a; } }
  • public:unproteced so can be accessible to all other classes private:protected so can not be accessible to only that class in which it is defined
  • These two classes definition shows the difference between the a class where all members are public versus a class where some members are private. This is a class where all members are public. 1. function PublicCats() { 2. // This is the list of cat names 3. this.nameList = []; 4. 5. // This is a method that I would like to be private but can’t 6. // It returns the last cat of the list 7. this.lastCat = function() { 8. return this.nameList[this.nameList.length-1]; 9. } 10. 11. // Return the list of names 12. this.names = function() { 13. return this.nameList; 14. } 15. 16. // Add a name to the list 17. this.add = function(name) { 18. this.nameList.push(name); 19. 20. // Return the last cat just added 21. return this.lastCat(); 22. } 23. } This is the corresponding class where some members are private. 1. function PrivateCats() { 2. // This is the list of cat names 3. var nameList = []; 4. 5. // This is a private method 6. var lastCat = function() { 7. // Note : I don’t use "this" to access private variables 8. // thanks to the power of closures! 9. return nameList[nameList.length-1]; 10. } 11. 12. // These are our public methods! 13. // This is where we create another scope to 14. // avoid external objects to use the private variables. 15. return { 16. add:function(name) { 17. // Note : once again, I don’t use "this" 18. // to access the private variables and methods 19. nameList.push(name); 20. return lastCat(); 21. }, 22. names:function() { 23. return nameList; 24. } 25. } 26. }
  • public methods, classes and variables: Any class can access them. protected indicates that only subclasses can access. package private indicates that only members within the same package have access. private indicates access only by the class that defines it. Rules of thumb: any public data members should be declared final, otherwise you run the risk of any class modifying a variable without any safety. If they must be a variable, make them private and allow access through public modifiers (get and set methods). In this way, you can monitor changes to variables or synchronise access, and take appropriate action. Keep all data members and methods private unless otherwise required. If a sub-class requires access to a super-class's method or member, protected access is a possibility, but consider access via protected get/set rather than making the variable protected. Package private methods are useful between classes that require access to each other, but who's functions have no significance outside of the package. Consider the use of making a constructor private: If only one instance of a class is required, a private constructor can only be called by that class. A static instantiation method can be used to create one and only one instance of a class. For small, single threaded applications, a lot of the rules can be broken without concern. It's when other programmers come into the mix, new threads are added that access variables at unpredictable moments, malicious code is introduced that quietly modifies something that it shouldn't... that's when one starts to appreciate access modifiers.

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy