Results 1 to 6 of 6
Thread: Constructors
- 08-17-2009, 11:53 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 60
- Rep Power
- 0
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!
-
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.
- 08-18-2009, 12:05 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 60
- Rep Power
- 0
Great! Thank you so much!
- 08-18-2009, 12:19 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
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(); } }
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!
- 08-18-2009, 12:32 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 60
- Rep Power
- 0
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.
- 08-18-2009, 07:46 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
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
-
Passing objects into constructors
By aaronfsimons in forum New To JavaReplies: 8Last Post: 04-14-2009, 01:08 PM -
Need some help with a task, constructors.
By JKM in forum New To JavaReplies: 1Last Post: 02-01-2009, 11:09 PM -
Help with constructors
By Minime in forum New To JavaReplies: 3Last Post: 04-09-2008, 08:59 AM -
Initializing variables using constructors
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 09:28 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 04:15 PM
Bookmarks