When to use Protected modifier
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
From the developer guides we have the following definition:
This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.
However, I am still unsure when to use this and how it differs from the public modifier.
apex access-modifier
add a comment |Â
up vote
1
down vote
favorite
From the developer guides we have the following definition:
This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.
However, I am still unsure when to use this and how it differs from the public modifier.
apex access-modifier
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
From the developer guides we have the following definition:
This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.
However, I am still unsure when to use this and how it differs from the public modifier.
apex access-modifier
From the developer guides we have the following definition:
This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.
However, I am still unsure when to use this and how it differs from the public modifier.
apex access-modifier
apex access-modifier
edited Sep 10 at 17:09
Adrian Larsonâ¦
101k19107226
101k19107226
asked Sep 10 at 16:04
M guy
31519
31519
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
The protected
access modifier is much more like private
than public
. In fact, for a class which is not virtual
nor abstract
, this access modifier would be the same as private (though it's not allowed). However, once you allow extension of the class, you can then see it in overriding implementations.
public class Class1
protected void foo()
// this access would just be the same as private
// however, the class will not compile
public virtual class Class2
protected void foo()
// now this class will compile
// this method can only be seen by extensions of this class
public class Class3 extends Class2
public static bar()
// cannot see the foo method here because the method is static
// the below line will cause compile fail
new Class1().foo();
public class Class4 extends Class2
public void baz()
// now that you are within an instance which extends the definition,
// you are able to see the method
this.foo();
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The protected
access modifier is much more like private
than public
. In fact, for a class which is not virtual
nor abstract
, this access modifier would be the same as private (though it's not allowed). However, once you allow extension of the class, you can then see it in overriding implementations.
public class Class1
protected void foo()
// this access would just be the same as private
// however, the class will not compile
public virtual class Class2
protected void foo()
// now this class will compile
// this method can only be seen by extensions of this class
public class Class3 extends Class2
public static bar()
// cannot see the foo method here because the method is static
// the below line will cause compile fail
new Class1().foo();
public class Class4 extends Class2
public void baz()
// now that you are within an instance which extends the definition,
// you are able to see the method
this.foo();
add a comment |Â
up vote
4
down vote
accepted
The protected
access modifier is much more like private
than public
. In fact, for a class which is not virtual
nor abstract
, this access modifier would be the same as private (though it's not allowed). However, once you allow extension of the class, you can then see it in overriding implementations.
public class Class1
protected void foo()
// this access would just be the same as private
// however, the class will not compile
public virtual class Class2
protected void foo()
// now this class will compile
// this method can only be seen by extensions of this class
public class Class3 extends Class2
public static bar()
// cannot see the foo method here because the method is static
// the below line will cause compile fail
new Class1().foo();
public class Class4 extends Class2
public void baz()
// now that you are within an instance which extends the definition,
// you are able to see the method
this.foo();
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The protected
access modifier is much more like private
than public
. In fact, for a class which is not virtual
nor abstract
, this access modifier would be the same as private (though it's not allowed). However, once you allow extension of the class, you can then see it in overriding implementations.
public class Class1
protected void foo()
// this access would just be the same as private
// however, the class will not compile
public virtual class Class2
protected void foo()
// now this class will compile
// this method can only be seen by extensions of this class
public class Class3 extends Class2
public static bar()
// cannot see the foo method here because the method is static
// the below line will cause compile fail
new Class1().foo();
public class Class4 extends Class2
public void baz()
// now that you are within an instance which extends the definition,
// you are able to see the method
this.foo();
The protected
access modifier is much more like private
than public
. In fact, for a class which is not virtual
nor abstract
, this access modifier would be the same as private (though it's not allowed). However, once you allow extension of the class, you can then see it in overriding implementations.
public class Class1
protected void foo()
// this access would just be the same as private
// however, the class will not compile
public virtual class Class2
protected void foo()
// now this class will compile
// this method can only be seen by extensions of this class
public class Class3 extends Class2
public static bar()
// cannot see the foo method here because the method is static
// the below line will cause compile fail
new Class1().foo();
public class Class4 extends Class2
public void baz()
// now that you are within an instance which extends the definition,
// you are able to see the method
this.foo();
edited Sep 10 at 16:32
answered Sep 10 at 16:16
Adrian Larsonâ¦
101k19107226
101k19107226
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231911%2fwhen-to-use-protected-modifier%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password