13 Replies Latest reply: Dec 4, 2007 12:37 PM by 796447 RSS

    arrays

    807600
      could anyone tell me why this code is not compiling? i want to create an array of Y1 and Y2 objects, and then just display some variables.

      something wrong with the constructor it seems
      class Y1 {
      
          int num;
      
          Y1(int n) {
           num = n;
          }
      }
      
      class Y2 extends Y1 {
      
          int num;
      
          Y2(int n) {
           num = n;
          }
      
          public static void main(String[] args) {
      
           Y1[] array = new Y1[3];
           array[0] = new Y1(2);
           array[1] = new Y2(3);
           array[2] = new Y2(6);
           
           System.out.println(array[1].num);
          }
      }
      Y2.java:14: cannot find symbol
      symbol : constructor Y1()
      location: class Y1
      Y2(int n) {
      ^
      1 error
        • 1. Re: arrays
          807600
          If you don't call super in a constructor, the compiler implicitly calls it with no parameters. So your Y2 constructor is actually this:
              Y2(int n) {
                  super();
               num = n;
              }
          Since Y1 has no no-arg constructor, this isn't valid. You can either a) call super(n) yourself, of b) create a no-arg constructor in Y1.

          You are also shadowing the variable num in Y2. If you declare a variable with the same name as a variable in the superclass, then the one from the superclass won't be visible, unless you refer to it explicitly with super.
          • 2. Re: arrays
            807600
            hunter9000 wrote:
            If you don't call super in a constructor, the compiler implicitly calls it with no parameters. So your Y2 constructor is actually this:
                Y2(int n) {
            super();
                 num = n;
            }
            Since Y1 has no no-arg constructor, this isn't valid. You can either a) call super(n) yourself, of b) create a no-arg constructor in Y1.

            You are also shadowing the variable num in Y2. If you declare a variable with the same name as a variable in the superclass, then the one from the superclass won't be visible, unless you refer to it explicitly with super.
            thanks hunter for the help, i changed my code and it seems to be working.
            i don't know though if this is exactly what you were meaning, is what i've done ok?
            class Y1 {
            
                int num;
            
                Y1(int n) {
                 num = n;
                }
            }
            
            class Y2 extends Y1 {
            
                int num;
            
                Y2(int n) {
                    super(n);
                }
            
            
                public static void main(String[] args) {
            
                 Y1[] array = new Y1[3];
                 array[0] = new Y1(2);
                 array[1] = new Y2(3);
                 array[2] = new Y2(6);
                 
                 System.out.println(array[1].num);
                 System.out.println(array[2].num);
                }
            }
            • 3. Re: arrays
              807600
              hunter9000 wrote:
              If you don't call super in a constructor, the compiler implicitly calls it with no parameters. So your Y2 constructor is actually this:
                  Y2(int n) {
              super();
                   num = n;
              }
              Since Y1 has no no-arg constructor, this isn't valid. You can either a) call super(n) yourself, of b) create a no-arg constructor in Y1.

              You are also shadowing the variable num in Y2. If you declare a variable with the same name as a variable in the superclass, then the one from the superclass won't be visible, unless you refer to it explicitly with super.
              thanks hunter for the help, i changed my code and it seems to be working, is what i've done ok, would you change anything?

              by the way, if this post appears twice it's just because my posts keep on disapperaing so i posted it twice
              class Y1 {
              
                  int num;
              
                  Y1(int n) {
                   num = n;
                  }
              }
              
              class Y2 extends Y1 {
              
                  int num;
              
                  Y2(int n) {
                      super(n);
                  }
              
              
                  public static void main(String[] args) {
              
                   Y1[] array = new Y1[3];
                   array[0] = new Y1(2);
                   array[1] = new Y2(3);
                   array[2] = new Y2(6);
                   
                   System.out.println(array[1].num);
                   System.out.println(array[2].num);
                  }
              }
              • 4. Re: arrays
                807600
                You define a Constructor Y1(int n) in your class, but did not define a default constructor Y();
                Java will only create a default constructor if there's no constructor defined.


                since your class extends Y1, it implicitly invoke the Y1() constuctor;
                    Y2(int n) {
                        //  invoke Y1()  but Y1() is undefined
                        // you can create a default constuctor Y1() in Y1 class or do super(n) to invoke the Y1(int n);
                     num = n;
                    }
                    
                • 5. Re: arrays
                  807600
                  i learnt from the above posts that my code needs a default constructor to compile.
                  All i know about them is that they set booleans to false and ints to zero etc.
                  The problem is i don't understand why this default constructor is needed and what it does in my code, why does the sub class need a super class with this constructor? could anyone please give me a little explanation of how it works?

                  Here is my code:
                  class Y1 {
                  
                      int num;
                  
                      Y1() {
                      }
                  
                      Y1(int n) {
                       num = n;
                      }
                  
                      void multiplyBy2() {
                          num *= 2;
                      }
                  }
                  
                  class Y2 extends Y1 {
                  
                      int i;
                  
                      Y2(int n) {
                       i = 2;
                      }
                  
                      public static void main(String[] args) {
                       Y1[] array = new Y1[3];
                       array[0] = new Y1(2);
                       array[1] = new Y2(3);
                       array[2] = new Y2(6);
                       System.out.println(array[1].num);
                       System.out.println(array[2].num);
                       array[0].multiplyBy2();
                       System.out.println(array[0].num);
                      }
                  }
                  • 6. Re: arrays
                    807600
                    i learnt from the above posts that my code needs a default constructor to compile.
                    All i know about them is that they set booleans to false and ints to zero etc.
                    The problem is i don't understand why this default constructor is needed and what it does in my code, why does the sub class need a super class with this constructor? could anyone please give me a little explanation of how it works?

                    Here is my code:
                    class Y1 {
                     
                        int num;
                     
                        Y1() {
                        }
                     
                        Y1(int n) {
                         num = n;
                        }
                     
                        void multiplyBy2() {
                            num *= 2;
                        }
                    }
                     
                    class Y2 extends Y1 {
                     
                        int i;
                     
                        Y2(int n) {
                         i = 2;
                        }
                     
                        public static void main(String[] args) {
                         Y1[] array = new Y1[3];
                         array[0] = new Y1(2);
                         array[1] = new Y2(3);
                         array[2] = new Y2(6);
                         System.out.println(array[1].num);
                         System.out.println(array[2].num);
                         array[0].multiplyBy2();
                         System.out.println(array[0].num);
                        }
                    }
                    • 7. Re: arrays
                      DarrylBurke
                      What Hunter told you at #1:

                      If you don't call super in a constructor, the compiler implicitly calls it with no parameters.

                      db
                      • 8. Re: arrays
                        807600
                        Darryl.Burke wrote:
                        What Hunter told you at #1:

                        If you don't call super in a constructor, the compiler implicitly calls it with no parameters.

                        db
                        yeah, i know the subclass constructor needs to call a superclass constructor. but what is the default constructor in class Y1 actually doing?, is it just initialising the num variable to zero?
                        • 9. Re: arrays
                          796447
                          yeah, i know the subclass constructor needs to call a superclass constructor. but what is the default constructor in class Y1 actually doing?, is it just initialising the num variable to zero?
                          Yes. You could easily test that and see.
                          • 10. Re: arrays
                            807600
                            in the below code i am not sure how the n variable in the A1 class is set to zero.
                            i know that the compiler creates a default constructor only if you do not create a constructor yourself. But i have a constructor in the A1 class but the n variable is still somehow set to zero. How does this happen if the A1 class does not have a default constructor?
                            could someone please clear this up for me? thanks!
                            class A1 {
                            
                                int n;
                            
                                A1(int x) {
                                    System.out.println("CLASS A1 PARAMETERIZED CONSTRUCTOR");
                                }
                            }
                            
                            class B1 extends A1 {
                            
                                B1(int x) {
                                 super(x);
                                 System.out.println("CLASS B1 PARAMETRISED CONSTRUCTOR");
                                }
                            }
                            
                            class C1 {
                            
                                public static void main(String[] arg) {
                                 B1 b = new B1(5);
                                 System.out.print(b.n); //prints 0
                                }
                            }
                            • 11. Re: arrays
                              796447
                              All class member variables are initialized to something, whether you explicitly initialize them or not. References are set to null, int values are set to 0, boolean values are set to false, etc.
                              So int this part:
                              int n; // = 0 by default
                              • 12. Re: arrays
                                807600
                                warnerja wrote:
                                All class member variables are initialized to something, whether you explicitly initialize them or not. References are set to null, int values are set to 0, boolean values are set to false, etc.
                                So int this part:
                                int n; // = 0 by default
                                thanks for your reply!
                                does this mean though that my A1 class does have a default constructor?, or sometimes a no args constructor is not needed to set certain variables to zero, false, null etc?
                                • 13. Re: arrays
                                  796447
                                  Your A1 class has only one constructor, which you defined. That constructor takes an argument, so it is not a "default constructor".

                                  A "default constructor" is a constructor which takes no arguments, also known as a "no-argument constructor". That has nothing to do with the behavior of setting members to default values if you don't explicitly set them.

                                  If you do not explicitly define any constructors yourself, then you get a default constructor generated for you, as if you had written it as:

                                  public YourClass() {}
                                  (which is equivalent to:)
                                  public YourClass() { super(); }

                                  If you code one or more constructors for a class, then it does NOT generate the default constructor for you.