Private Class Declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AtulTiwari
    New Member
    • May 2007
    • 23

    Private Class Declaration

    Can any one here who tell me that why we can not Declare a class or Interface as Private or Protected.

    I have just compiled a class as

    private class abc{
    }

    then the compiler throw an error that private is not allowed here

    So can any one plz explain me the reason behind that?
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Java is not my strongest language, however I would suggest that it doesn't make sense for a class to be private or protected, because it means that (almost) noone can see it.......

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by AtulTiwari
      Can any one here who tell me that why we can not Declare a class or Interface as Private or Protected.

      I have just compiled a class as

      private class abc{
      }

      then the compiler throw an error that private is not allowed here

      So can any one plz explain me the reason behind that?
      A class can be decared as protected, in which case it will only be available in the package in which it is declared.
      A private class does not make sense and is not allowed.
      You can read this article for a descrption of some of this stuff.

      Comment

      • AtulTiwari
        New Member
        • May 2007
        • 23

        #4
        Originally posted by DeMan
        Java is not my strongest language, however I would suggest that it doesn't make sense for a class to be private or protected, because it means that (almost) noone can see it.......

        Thanks for your Reply,

        Actually I am also thinking the same from my own, but i have Posted the Question just bcz to know that my thinking is right or wrong.

        But if any one wants to explain something more then, Most Welcome,

        Thanks Again

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          I think r0 has probably provided a better explanation and a very useful link......

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by DeMan
            I think r0 has probably provided a better explanation and a very useful link......
            You call me r0 one more time and you can consider yourself banned.

            Comment

            • KishoreM
              New Member
              • Jun 2007
              • 12

              #7
              Hi,

              first thing It is not right that you can not create a private or protected class.....you can create but this class should not be Top level class in the file means you can create private/protected innerclasses.

              second thing what is the sense to create a Top level class as private/protected because anyway it is not going to be accessed by outside programs.

              regards,

              Kishore M

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by KishoreM
                Hi,

                first thing It is not right that you can not create a private or protected class.....you can create but this class should not be Top level class in the file means you can create private/protected innerclasses.

                second thing what is the sense to create a Top level class as private/protected because anyway it is not going to be accessed by outside programs.

                regards,

                Kishore M
                OK.
                A protected class can be accessed by other programs within the same package that that class has been defined (so it can be defined top level).
                A private top level class is not allowed because that's not going to be of any use but you can declare a private class as a member of another class.

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  Originally posted by r035198x
                  You call me r0 one more time and you can consider yourself banned.
                  sorry, like a phone number I forgot the digits.......

                  Comment

                  • AtulTiwari
                    New Member
                    • May 2007
                    • 23

                    #10
                    Originally posted by DeMan
                    sorry, like a phone number I forgot the digits.......


                    Sorry sorry but i found a Quote in the link given here
                    http://www.thescripts. com/forum/thread641524.ht ml

                    and it will explaining that

                    Class structure

                    A class declaration starts with a modifier which can be any of
                    public, protected, private, abstract, static, strictfp, final.
                    To get us started we'll look at the public and protected modifiers first.
                    Java classes are encapsulated into packages. Classes in one package can only access public classes in another package. Protected classes are accessible only within the package that they have been defined. If you do not include a modifier, protected access is assumed. The class is put inside the default package which is automatically created for you.
                    You should always put your classes in a package. Here is the rest of the structure of a Java class:

                    Modifier(s) class ClassName {
                    members(methods , fields, nested classes and interfaces)
                    constructors
                    static initializers

                    }



                    Now what you all says about this article?

                    Comment

                    • AtulTiwari
                      New Member
                      • May 2007
                      • 23

                      #11
                      Originally posted by r035198x
                      OK.
                      A protected class can be accessed by other programs within the same package that that class has been defined (so it can be defined top level).
                      A private top level class is not allowed because that's not going to be of any use but you can declare a private class as a member of another class.

                      Sorry r0 i want to disturb you here but we can not use protected as modifier for top level class and it will give compile time error.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by AtulTiwari
                        Sorry sorry but i found a Quote in the link given here
                        http://www.thescripts. com/forum/thread641524.ht ml

                        and it will explaining that

                        Class structure

                        A class declaration starts with a modifier which can be any of
                        public, protected, private, abstract, static, strictfp, final.
                        To get us started we'll look at the public and protected modifiers first.
                        Java classes are encapsulated into packages. Classes in one package can only access public classes in another package. Protected classes are accessible only within the package that they have been defined. If you do not include a modifier, protected access is assumed. The class is put inside the default package which is automatically created for you.
                        You should always put your classes in a package. Here is the rest of the structure of a Java class:

                        Modifier(s) class ClassName {
                        members(methods , fields, nested classes and interfaces)
                        constructors
                        static initializers

                        }



                        Now what you all says about this article?
                        I hope you're not starting to get confused.
                        A private class cannot be top level. It can only be declared inside another class. That's why private was listed there as one of the possible class modifiers.

                        Comment

                        • AtulTiwari
                          New Member
                          • May 2007
                          • 23

                          #13
                          Originally posted by r035198x
                          I hope you're not starting to get confused.
                          A private class cannot be top level. It can only be declared inside another class. That's why private was listed there as one of the possible class modifiers.

                          thanks to all of you for your kind discussion here.
                          I really got very useful knowledge from here.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by AtulTiwari
                            Sorry r0 i want to disturb you here but we can not use protected as modifier for top level class and it will give compile time error.
                            Yack, I've really messed things up now.

                            protected won't work for top level class alright.
                            If you leave a class without a modifier (and define no package), it is placed in the default package and is accessible only inside that default package (sort of protected access for that class).
                            But you're all right, protected specifier won't work for top level and I've been telling it wrong there.

                            Edit: You can call me r0 now after that mistake.

                            Comment

                            • DeMan
                              Top Contributor
                              • Nov 2006
                              • 1799

                              #15
                              .....(does that include me)

                              Comment

                              Working...