tags:

views:

145

answers:

4

Will public access modifiers limits flexibility in changing code?If so give some examples..

A: 

If you can access the source code the modifiers can not limit your flexibility because you can change the modifiers.

If you can not access the source code you cannot extend or override any class or method marked final, you cannot access any method marked private, or default (without modifier) if your class is outside of the origin package, or protected if your class is outside of the origin package and not extends the original class

Telcontar
Of course it can limit your flexibility, if you have to remain compatible with code that compiled against an earlier version of your code. Then you can't easily change the modifiers.
Joachim Sauer
Well, i was thinking in broading the modifiers (private to protected for example)... but it's true that if you have extended a class with a private method and you implement a method withs the same name in a extended class... if you changes the private modifier to protected it can be undesired results. But this is too infrecuent... i hope
Telcontar
@Telcontar - the question is specifically about "public" access, not about modifiers in general.
Stephen C
A: 

The first two answers from googling your question pretty much say it all

java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

www.java-samples.com/showtutorial.php?tutorialid=655

Now my perspective: I guess it is valuable to add that it is good practice to start with private by default and escalate to protected when if the class is extended; and public when you want to grant access to classes from different packages.

However, there is nothing stopping you from giving all members a public declaration. This does quite the opposite and increases your flexibility; with the caveat being that you sacrifice security. For example, if a 3rd party loads their own classes together with yours in the same classpath, and all the members in your class are public, they are easily able to modify the state of your classes. Numerous other examples.

So, unless you are absolutely unconcerned about security, escalate modifiers as and when needed.

bguiz
@bguiz - in general, Java access modifiers have no impact on security because reflection allows you to ignore them; e.g. to read a private attribute of any class. The only case where access modifiers provide any security is in a sandbox that disables the relevant reflection methods.
Stephen C
A: 

Yes definitely public access modifiers limits flexibility in changing code with encapuslation feature in OOP . let me try explaining with an example . As you know the scope of the public access modifier can be accessed from any of the classes regardless of the package which they belong to . which results in Bad Object oriented design.

package com.app.access;

public class  Car{
  public int wheels;
  public int lights;
  //  Method for how wheels rotate
 // Method for how lights on
}

public class TestCar{
  public static void main(String [] args) {

      Car c = new Car();
      c.wheels = -5; // Legal but bad 00 because we know it cannot have a negative value
   }
}

Above example is Bad OO design which should be avoid by making a good practice providing private or protected access modifier to the instance variable and access through the Java bean compliant accessor methods take the form

get<propertyName> or for booleans is<propertyName> and set<propertyName>

provide a place to check and/or validate before returning or modifying a value.

Hope this Helps !!

YetAnotherCoder
Though its bad practice, the use of `public` is not what limits flexibility here. The thing that limits flexibility >>here<< is that the example doesn't use getters and setters!
Stephen C
To illustrate what I mean, consider the same example with `wheels` declared as `protected`. Same problem ... no place to interpose validation code etc once other classes have been coded against the `Car` API.
Stephen C
@Stephen i was explaining how it limit the flexibility of the Code and i just tried to explain how it limits the flexibilty of the Code with this example . Since this is a home work i thought by providing the example which limits the flexibility would give him clue to write homework for Good design :)
YetAnotherCoder
A: 

There is no doubt that it is a bad idea to declare some class or member to be public when your OO design dictates that it should have more restricted access.

But does this limit your flexibility? I'd say yes.

Since this is homework, I'm not going to spell out the answer for you ... or give you examples that you can cut and paste. But consider Java's rules for overriding members in a subclass. Specifically, consider what you can and cannot do in terms of changing the access on the method.

Stephen C