Results 1 to 3 of 3

Thread: Problem extending a class

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158

    Unhappy 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);
            }
        }
    
        //--------------------------------------------------------------------------
    }

  2. #2
    VirtuallyVB
    Guest
    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".

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width