This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
multiple constructor - answer
Posted by Y.M.Sudarsan on August 08, 2000 at 10:13 AM
> In a Multiple constructor class, a constructor can call another constructor. How to call it? > Assume there are three constructors in the class you consider. > Please write a class that contains: > a) three constructor (and some other codes) > b) the third constructor calls the second constructor and the second one calls the first > write a sub-class (extends the first class) that contains one constructor invoking > one of constructor in the super-class. 0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0o0 The solution for you problem is as seen in the following code. You invoke the constructor by ceating a new object. //code by Y.M.Sudarsan sonaali@rediffmail.com class Constructor { public static void main(String args[]) { second s1 = new second(); } } class f { f() { System.out.println("empty cons"); } f(int i) { System.out.println("cons with i "+i); new f(); } f(String s) { System.out.println("cons with s "+s); new f(10); } } class second extends f { second() { super("test constructor"); } }
Replies:
|