Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Constructor chaining

843789Sep 7 2009 — edited Sep 8 2009
Hi Friends,
I am new to java,I am using Eclipse IDE to work in java..Below i have written a program,in which it shows error while we extends Class Point in Class Message,It shows error that " Implicit super constructor Point() is undefined for default constructor. Must define an explicit constructor"..why i should explicit?but if u remove parameter in Point constructor,it is not showing error.

class Sample
{
Sample()
{
System.out.println("This is class Sample");
}
}

class Point extends Sample
{
int x=25;
int y=35;
Point(int x,int y)
{

this.x=x;
this.y=y;
System.out.println(x+y);
}
}

public class Message extends Point
{
public static void main(String args[])
{

}
}

If i explicitly include constructor with super keyword with parameter as shown in below,it is not showing error.

class Message extends Point
{
int z=45;
Message(int x,int y,int z)
{
super(x,y);
this.z = z;
System.out.println("This is main class constructor");
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
public static void main(String args[])
{
Message M = new Message(1,2,3);

}

what is the reason?please tell me..Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 6 2009
Added on Sep 7 2009
5 comments
523 views