Q: Constructors Polymirphisms

O

Omer

Hi All,
In my class I'd like to have 2 constructors with different signature
(arguments). I'd like one to call the other, something like the
following example.

//------------------------------------------------------------------
import java.awt.Point;
public class MyClass {
Point pt;
//------------------------------------------------------------------
public MyClass (int x, int y) {
pt = new Point(x, y);
}
//------------------------------------------------------------------
public MyClass () {
MyClass (0, 0);// <=== error: cannot resolve symbol. :-(
}
//------------------------------------------------------------------
}
//------------------------------------------------------------------

This code fails on "cannot resolve symbol." on the marked line.
Any suggestion?
TIA,
Omer.
 
L

Lew

Omer said:
public MyClass () {
MyClass (0, 0);// <=== error: cannot resolve symbol. :-(
}

Don't embed TAB characters in Usenet posts.
public MyClass () {
this (0, 0);
}

Side note to the OP: this is not polymorphism but constructor overloading.
Polymorphism involves method overrides, which don't happen with constructors.

Why'n'cha check out the Wikipedia entry for "Type polymorphism"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top