|
-
Jul 8th, 2001, 06:56 AM
#1
Thread Starter
Addicted Member
Problem extending a class
Hi I have coded the following class which is ment to extend the registeredList class (shown in my previous post) but I get an error when trying to run the program, it says
Error (7) constructor RegisteredList() not found in class AirTrafficControl.RegisteredList.
Any ideas any one?
Code:
public class CirclingList extends RegisteredList
{
//--------------------------------------------------------------------------
public CirclingList(int maxPlanes)
{
list = new Airplane[maxPlanes];
// initialise list
for (int i = 0; i < list.length; i++)
{
list[i] = new Airplane("", 0);
}
}
//--------------------------------------------------------------------------
}
-
Jul 8th, 2001, 02:23 PM
#2
Without an explicit call within the subclass to a particular baseclass constructor, OO languages tend to call the default constructor of the baseclass implicitly. If this default constructor does not exist, you get the error.
A default constructor YourClass() is automatically (by default) supplied when no constructor is given, but once you supply a non default constructor, you don't get the default one supplied, so you must explicitly supply it.
class BaseClass{
public BaseClass(int i){}//public BaseClass(){} does not exist!
}
class SubClass extends BaseClass{
public SubClass(float f){}//will fail compilation BY CALLING BaseClass()
public SubClass(float f){super((int)f;}//will pass compilation because it explicitly calls the constructor requiring an int
}
Either add public BaseClass(){} to BaseClass or explicitly call another BaseClass constructor to allow it to be "found".
-
Jul 9th, 2001, 11:08 AM
#3
Dazed Member
Yes, always remember. "Super is often used to invoke a superclass constructor from within a subclass constructor"
Note~* If super() appears in a constructor it must appear as the first statement in the constructor.
example:
import java.awt.Color;
public class MyBox extends Box{
Color outerColor;
Color innerColor;
public MyBox(double x, double y, double width, double height, Color outer, Color inner){
super(x,y,width,height);
outer = outerColor;
inner = innerColor;
}
}
public MyBox(Color outer Color inner){
super(10,10,100,100);
outerColor = outer;
innerColor = inner;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|