When to use Protected modifier

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question





























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 10 at 17:09









      Adrian Larson♦

      101k19107226




      101k19107226










      asked Sep 10 at 16:04









      M guy

      31519




      31519




















          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();







          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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();







            share|improve this answer


























              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();







              share|improve this answer
























                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();







                share|improve this answer














                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();








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 10 at 16:32

























                answered Sep 10 at 16:16









                Adrian Larson♦

                101k19107226




                101k19107226



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?