protected Access Specifier
hi people ,
thanks for support . but i have Simple Question in Access Specifier
my question is here:
Can i change the value of a member declared as protected in the main ??
class A {
protected String name;
}
class B extends A{
}
public class x{
public static void main (String arg[]){
B obj = new B ();
obj.name = "Hello";
}}
--if it is possible, then how the member is protected ???
Re: protected Access Specifier
I suggest you read about the protected keyword and see what it means to be protected. While you are there check out the other options. It will all be clear if you read that one page..
Re: protected Access Specifier
No, you can't do that. I suggest you look into using a getter method. You don't need to define one in A so you can't access name publicly with A, but with B you can access name publicly.
Re: protected Access Specifier
I think helloworld922 misunderstood. Perhaps I misunderstood. Maybe this will clear it up:
Code java:
public class A {
protected String name;
private String surname;
}
public class B extends A {
}
public class X {
public static void main(String args[]) {
B object = new B();
//protected
object.name = "Hello";
System.out.println(object.name);
//private
//object.surname = "World"; //Will not work!
//System.out.println(object.surname); //Will not work!
}
}
The object.name will work as I believe you suggested in your first post. Now that is to assume you have the classes in the same package. Read this page.
Re: protected Access Specifier
thanks .. for explanation .
it means protected members are visible to the main . as long as it is the same package..
Re: protected Access Specifier
Quote:
Originally Posted by
spark
...it means protected members are visible to the main...
Protected would not just be visible to main...
Protected is visible to the entire class where it is declared. It is visible to the entire class of any class that extends the original class. It is visible to the entire class of any class in the same package the original class is in. Private would be the keyword that would offer the "protection" I think you expected from "protected String name;" in your original post.
Re: protected Access Specifier
Re: protected Access Specifier
edit: My mistake. I originally thought protected disallowed package access. This is incorrect. Edited post to remove offending content.
Re: protected Access Specifier
Quote:
Originally Posted by
helloworld922
Minor correction: protected is
not visible to any other class in the same package. It is only visible to that class and classes which extend that class. There is another visibility mechanism which allows this behavior (the 'default' visibility). This behavior is used when no modifier is used.
Code java:
package test;
public class A
{
protected String name;
String address;
}
Code java:
package test;
public class Main
{
public static void main(String[] args)
{
A a = new A();
a.address = "123 fake street"; // ok since they're in the same package
a.name = "Spock"; // compile error
}
}
I disagree completely. Your code compiles as is.
...and with an output:
Code java:
package test;
public class A
{
protected String names;
String addresses;
}
Code java:
package test;
public class Main
{
public static void main(String[] args)
{
A a = new A();
a.addresses = "123 fake street"; // ok since they're in the same package
a.names = "Spock"; // NOT a compile error
System.out.println(a.addresses + " " + a.names);
}
}
Output:
Quote:
123 fake street Spock
Re: protected Access Specifier
Re: protected Access Specifier
Quote:
Originally Posted by
pbrockway2
Ya that is the same link I posted which states:
Quote:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
Re: protected Access Specifier
Sorry, I didn't see the link.
Re: protected Access Specifier
Quote:
Originally Posted by
jps
I disagree completely. Your code compiles as is.
...and with an output:
Code java:
package test;
public class A
{
protected String names;
String addresses;
}
Code java:
package test;
public class Main
{
public static void main(String[] args)
{
A a = new A();
a.addresses = "123 fake street"; // ok since they're in the same package
a.names = "Spock"; // NOT a compile error
System.out.println(a.addresses + " " + a.names);
}
}
Output:
blah, my bad. You're totally correct. The only excuse I have is a massive brain fart on my part :( I forget that Java packages behave differently from namespaces and assemblies.
Re: protected Access Specifier
in the same package it is accessible
Re: protected Access Specifier