5 Replies Latest reply: Mar 30, 2013 3:48 PM by Madeline RSS

    Need understanding for public and private.

    998915
      I have pasted the code, now my query is that in 1st class named "accesssp" I have added object and then written SOP then why cant I have output as value of B ??




      class accesssp     {
           
           public int a=56;
           private int b=5566;
           public int c=58766;
           
           accesssp sp2 = new accesssp();
           System.out.println(b);

      }

      class accesssp1 extends accesssp{

           public void accessd()     {
                
                System.out.println(a);
                System.out.println(c);
                
           }
           
      }
      public class Access_Spf {

           public static void main(String[] args) {


                accesssp1 sp1 = new accesssp1();
                sp1.accessd();
           }

      }

      Edited by: 995912 on Mar 25, 2013 2:51 AM
        • 1. Re: Need understanding for public and private.
          gimbal2
          The code doesn't even compile. Don't post code that looks like what you have, post the exact code you have. And please use \
           tags.                                                                                                                                                                                                                                                                                
          • 2. Re: Need understanding for public and private.
            998915
            first of all thanks for reply,
            this is my first post so sorry for mistake.

            and code is only what i have posted there is not any more.

            I have doubt in that portion in first class.....

            Edited by: 995912 on Mar 25, 2013 2:49 AM
            • 3. Re: Need understanding for public and private.
              Kayaman
              995912 wrote:
              and code is only what i have posted there is not any more.
              But the code doesn't compile. It's wrong. Don't past code that doesn't compile, it's not useful.
              • 4. Re: Need understanding for public and private.
                gimbal2
                995912 wrote:
                and code is only what i have posted there is not any more.

                I have doubt in that portion in first class.....
                No wonder, there isn't even any method in there. You should not be asking questions in a forum, you should be reading a book on Java and following its examples.
                • 5. Re: Need understanding for public and private.
                  Madeline
                  Pratik_Ambani wrote:
                  I have pasted the code, now my query is that in 1st class named "accesssp" I have added object and then written SOP then why cant I have output as value of B ??




                  class accesssp     {
                       
                       public int a=56;
                       private int b=5566;
                       public int c=58766;
                       
                  accesssp sp2 = new accesssp(); //You cannot do this here. It will give you an error!
                  System.out.println(b); // You have a class body but no method. You should put this in a method!

                  }

                  class accesssp1 extends accesssp{

                       public void accessd()     {
                            
                            System.out.println(a);
                            System.out.println(c);
                            
                       }
                       
                  }
                  public class Access_Spf {

                       public static void main(String[] args) {


                            accesssp1 sp1 = new accesssp1();
                            sp1.accessd();
                       }

                  }

                  Edited by: 995912 on Mar 25, 2013 2:51 AM
                  You could do something like this:
                  class accesssp     {
                       public int a=56;
                       private int b=5566;
                       public int c=58766;
                       accesssp sp2;
                       
                       public void youNeedThis(){
                            sp2 = new accesssp();
                            System.out.println(b);     
                       }
                       
                  }

                  class accesssp1 extends accesssp{

                       public void accessd()     {
                            System.out.println(a);
                            System.out.println(c);
                       }
                  }
                  public class Access_Spf {

                       public static void main(String[] args) {
                            accesssp1 sp1 = new accesssp1();
                            sp1.accessd();
                       }

                  }

                  Edited by: Madeline on Mar 30, 2013 9:45 PM

                  Edited by: Madeline on Mar 30, 2013 9:46 PM