Results 1 to 6 of 6

Thread: Constructors

  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default Constructors

    Exception has only 4 constructors:
    public Exception()
    public Exception(String message)
    public Exception(String message, Throwable cause)
    public Exception(Throwable cause)

    If I have 'CheckedException extends Exception', and "ResourceException extends CheckedException".

    Does it mean that ResourceException can have only 4 constructor also exactly like the above? Is it possible for me to create a 5th constructor, maybe
    public Exception(String message, String message, Throwable cause)?

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your child class can have as many number of constructors as it pleases since the number of parent constructors in no way restricts the number of child constructors. If I were you, I'd look in the API for the number of and format of the constructors for ResourceException. This will become important when you want to call the super class's constructor from within your child's constructor.

  3. #3
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    Great! Thank you so much!

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You can have as many constructors as you want. If a super-class has, say, 6 constructors, your sub-class could have 20 or 1,7,5,4,10,200 if you wanted. (or even 0, but then the default constructor is assumed)
    If the super-class has a no-args constructor, that is automatically called, but if you explicitly call super, you'll call another one.
    e.g
    Java Code:
    public class ConstructorNumbers {
    	class Super{
    	     protected String arg;
    	     Super(String anArg){
    	          arg = anArg;
    	     }
    	}
    	class Sub extends Super{
    		protected String subArg;
    		Sub(String arg0,String arg1){
    			super(arg0);
    			subArg = arg1;
    		}
    		Sub(){
    			super("Default arg from Sub");
    			subArg = "Default subArg";
    		}
    		Sub(String[] args){
    			super(args.length>0 ? args[0]:"Default arg from Sub");
    			subArg = args.length>1 ? args[1]:"Default subArg";
    		}
    	}
    	class SubSub extends Sub{
    		
    		SubSub(String[] args){
    			super(args);
    		}
    		SubSub(){}//calls Sub() implicitly
    		public void print(){
    			System.out.println("anArg = " + arg + "\nsubArg = " + subArg);
    		}
    	}
    	public static void main(String[] args){
    		new ConstructorNumbers().new SubSub(args).print();
    	}
    }
    EDIT: Missed the last couple posts because I got stuck doing something, but this should help :D
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    Thank you very much Singing Boyo. I know nothing about java ... it will take me some time to understand your example... I really do appreciate it though.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm... well, the base of it is, there is no restriction on the number of constructors a class can have, and any constructor can call any super-constructor. (A constructor in a sub-class can call any constructor from the super-class)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 01:08 PM
  2. Need some help with a task, constructors.
    By JKM in forum New To Java
    Replies: 1
    Last Post: 02-01-2009, 11:09 PM
  3. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 08:59 AM
  4. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:28 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 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
  •