Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By gimbal2
  • 1 Post By jim829

Thread: Implicit Superclass constructor MyNumber() is not defined, must invoke another constr

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Implicit Superclass constructor MyNumber() is not defined, must invoke another constr

    I am getting the error mentioned in title in the following code. Please tell me why am I getting this error, although i haven't called a default constructor MyNumber() from the superclass any where, and how to fix it.

    Java Code:
    package referencereturntype;
    
    
    public class MyNumber {
    	String num;
    	
    	public MyNumber(String str){
    		num=str;
    	}
    	
    	public static void main (String [] args){
    		MyNumber my_num= new MyNumber("+2");
    		System.out.println("Success! The object of the class itself is successfully returned from retOb(). The object now contains the string : " + retOb(my_num).num);
    	}
    	
    	public static SubMyNumber retOb(MyNumber my_num){
    		SubMyNumber sub_my_num= new SubMyNumber("-50");
    		sub_my_num.nums=my_num.num;
    		return sub_my_num;
    	}
    }
    
    
    public class SubMyNumber extends MyNumber {
    	String nums;
    	
    	public SubMyNumber( String strs){
    		nums=strs;
    	}	
    }


    Thanks in advance.
    Don't forget to smile :-)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    The default constructor of a super class for class Foo is Foo(). However, if you explicitly add your own constructor, then the default constructor "goes away" and you have to re-add it. Why is this important? Because when you extend class Foo, the sub class constructor will attempt to call the default constructor of the super class (unless you specify an alternate super class constructor). If it is not there, the compiler complains.

    Regards,
    Jim
    Last edited by jim829; 10-28-2013 at 08:28 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    To extend a little:

    If you create your class and you don't need any specific constructor behavior i.e. you plan to instantiate your class with new Foo() constructor, you don't even need to declare it in your code - the compiler will create (default) one for you.

    Once you create your own constructor for specific call i.e. new Foo(String s) - compiler will not create default one. If you need it in your code, you will have to create it on your own...

  4. #4
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    If you need it in your code
    This! I don't need it. i haven't needed or called a default constructor anywhere in my code. I haven't created any instances which are initialized by a no argument/ default constructor.
    Don't forget to smile :-)

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    Your code says otherwise. It is a compilation error. In your subclass you have the following:

    Java Code:
    public class SubMyNumber extends MyNumber {
        String nums;
         
        public SubMyNumber( String strs){
            nums=strs;
        }   
    }
    There is an implicit call to super() in your subclass constructor so it behaves like this.

    Java Code:
    public class SubMyNumber extends MyNumber {
    
        String nums;
         
        public SubMyNumber( String strs){
            super();
            nums=strs;
        }   
    }
    But there is no default constructor in your super class. You must add it.

    Regards,
    Jim
    milovan likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    There is an implicit call to super() in your subclass constructor so it behaves like this.
    publicSubMyNumber( String strs){ super();
    nums=strs;
    }

    Should I learn that a constructor of a subclass on the first line of its implementation implicitly calls the default constructor of the super class?? I think I was told that or read it somewhere I don't remember properly.
    Don't forget to smile :-)

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    leave out the "first line" part, otherwise you end up with magic invisible code. When constructing an object, the Java runtime will implicitly invoke the entire chain of constructors for you.

    You can try it out yourself to see it in action: simply create some subclasses and put System.out.println statements in them, then see what gets printed when constructing an object and in which order.
    Nazneen Ali likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    You can try it out yourself to see it in action: simply create some subclasses and put System.out.println statements in them, then see what gets printed when constructing an object and in which order.
    Cool. I have tried it using the following code, and it worked. The object of the sub class did invoke the no value constructor of the super class when it was present, otherwise it generated the error.
    1. But you said,
    the Java runtime will implicitly invoke the entire chain of constructors for you.
    But it only invoked the no args constructor of the super class, it did not invoke the other constructors of super class (What did you mean by "entire chain of constructors")


    Java Code:
    package constructors_invoked;
    
    public class Constructors {
        public Constructors(){
            System.out.println("Default constructor invoked! \n\n");
        }
        
        public Constructors (String a){
            System.out.println(a + "1 value constructor invoked! \n\n");
        }
        
        public Constructors (String a, String b){
            System.out.println(a+a+"2 values constructor invoked! \n\n");
        }
        
    /*    public static void main(String [] args){
            Constructors con0= new Constructors();
            Constructors con1= new Constructors("");
            Constructors con2= new Constructors("","");
        }*/
    }
    
    public class Sub extends Constructors {
        public Sub (String c, String d, String e, String f){
            System.out.println(c+d+e+f + "\n The only constructor of subclass (4 values constructor) invoked!\n\n");
        }
    
    
        public static void main(String [] args){
            Sub sub4= new Sub("","","","");
            
        }
    }
    OUTPUT: -

    Default constructor invoked!






    The only constructor of subclass (4 values constructor) invoked!

    2. Moreover, if I remove the no args constructor from the super class and add a call to "any" constructor of the super class (which is present there) in the first line of implementation of the constructor of the subclass, it does not complain about the no args constructor of super class being undefined.

    This means whenever an object of a subclass is created, its constructor needs to call any constructor of the super class, if not, java run time makes an implicit call to the default constructor of the super class. So if it doesn't find it there, it complains. Am I right?
    Last edited by Nazneen Ali; 10-29-2013 at 04:50 AM.
    Don't forget to smile :-)

  9. #9
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: The Idiot's Help Guide To Mulberry bags Explained

    The Idiot's Help Guide To Mulberry bags Explained
    Deleted spam quote.
    What??
    Last edited by Tolls; 10-29-2013 at 10:07 AM.
    Don't forget to smile :-)

  10. #10
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    94
    Rep Power
    0

    Default Re: The Idiot's Help Guide To Mulberry bags Explained

    Quote Originally Posted by Nazneen Ali View Post
    What??
    Spam....

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    Quote Originally Posted by Nazneen Ali View Post
    But it only invoked the no args constructor of the super class, it did not invoke the other constructors of super class (What did you mean by "entire chain of constructors")
    No it won't invoke all the constructors of a given class. That would not make sense. But it will invoke constructors all the way up the hieirarchy of the class. If a specific constructor is not specified, then the default constructor of the super class will be invoked implicitly.

    Quote Originally Posted by Nazneen Ali View Post
    This means whenever an object of a subclass is created, its constructor needs to call any constructor of the super class, if not, java run time makes an implicit call to the default constructor of the super class. So if it doesn't find it there, it complains. Am I right?
    Yes. Except that it's the compiler that complains, not the JVM.

    Regards,
    Jim
    Nazneen Ali likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Implicit Superclass constructor MyNumber() is not defined, must invoke another co

    Thank you very much @ jim829
    Don't forget to smile :-)

Similar Threads

  1. Superclass and Subclass Constructor Question
    By achiu17 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 05:05 AM
  2. Is there anyway not to call superclass constructor ?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 09:01 PM
  3. Having trouble with inheriting constructor from superclass
    By coasterguy10 in forum New To Java
    Replies: 3
    Last Post: 10-20-2011, 05:07 AM
  4. Replies: 3
    Last Post: 01-21-2011, 01:37 PM
  5. y superclass constructor is called automatically
    By _ShivamKapoOr_ in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 01:58 PM

Posting Permissions

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