Results 1 to 12 of 12

Thread: Problem with constructor overloading

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843

    Problem with constructor overloading

    I have this code


    class sobre {


    sobre (int x)
    {
    System.out.println(x);
    }

    sobre (int x, int y)
    {
    sobre(x);
    }

    public static void main(String arguments[])
    {
    sobre s=new sobre(3,21);
    }

    }


    Wich is overloading the constructor... but it tells me C:\Program Files\Xinox Software\JCreator Pro\MyProjects\aaa\sobre.java:12: cannot resolve symbol
    symbol : method sobre (int)
    location: class sobre
    sobre(x);


    WHY!!! It should work!!!
    "The difference between mad and genius is the success"

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    try this...
    Code:
    class sobre { 
    
    
    sobre (int x) 
    { 
        System.out.println(x); 
    } 
    
    sobre (int x, int y) 
    { 
        this(x); 
    } 
    
    public static void main(String arguments[]) 
    { 
        sobre s=new sobre(3,21); 
    } 
    
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    you are the man....

    good lord man... you know vb and java... are you an oracle or what?!?!?!
    "The difference between mad and genius is the success"

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Muwahahahahaha!!!

    I am master of all!!!

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Andreex somthing things you might want to
    take note of. The this() construct when
    used in this manner is called a "constructor call
    statement" Using this() in this manner enables
    local chaining of constructors. Java places restrictions
    on how it is to be used. For instance this can only be
    used in a constructor definition. Also this() must appear
    as the first statment in a constructor.

    Using super() allows your subclass to
    influence the intialization of it's inherited state when
    an object of the subclass is created. Some of the same
    restrictions apply. super() can only appear as the
    first statement in a constructor. So basically this()
    and super()cannot occur in the same constructor.
    Code:
    class Light {
         private int watts;
         private boolean indicator;
         
        Light(){
          this(300,false);
          System.out.println("Returning from non-default constructor 1 in class Light");
           }
    
        Light(int watts, boolean indicator){    
          super(); // implicitly placed in if not supplied 
           this.watts = watts;
            this.indicator = indicator; 
           System.out.println("Returning from non-default constructor 2 in class Light");
           } 
    }
       class HighPressureSodiumLamp extends Light{
          
        private int heatDisplacement;
     
       HighPressureSodiumLamp(){
         this(600,300,false);
          System.out.println("Returning from non-default constructor 1 in class HighPressureSodiumLamp");
          }
       
        HighPressureSodiumLamp(int heatDisplacment,int watts, boolean indicator){
        super(300,false);
           this.heatDisplacement = heatDisplacment;
             System.out.println("Returning from non-default constructor 2 in class HighPressureSodiumLamp");
          }
       }
    
     class Test{
       public static void main(String[] args){
     	   HighPressureSodiumLamp hps = new HighPressureSodiumLamp();
       } 
     }

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    So... the code up here.... wouldnt work????

    That's very intereseting,,,, thanks for the note!
    "The difference between mad and genius is the success"

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    Humm..... I did something similar and it did work....
    "The difference between mad and genius is the success"

  8. #8

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    ?????? what was that?!!?
    "The difference between mad and genius is the success"

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    ?????? THAT IS WIERD!!!

    A MESSAGE IN MY MSN MESSEANGER CAME UP I HAD A NEW EMAIL FORM VBWORLD AND I CAME IN THRU THE LINK... IT BROUGHT ME TO THIS POST... AND YOU (DILIGENT) POSTED A PIC OF A MAGIC LAMP... BUT.. NOW I CAME DIRECTLY TO VBFORUMS AND THAT POST (YOURS) IS GONE... HOW CAN YOU DO THAT?!


    AM I ON DRUGS OR WHAT?!?!?!
    "The difference between mad and genius is the success"

  11. #11

  12. #12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width