5 Replies Latest reply: Sep 17, 2008 4:03 PM by 843785 RSS

    Compiler complaining about superclass' constructor not present in subclass

    843785
      I have a class Map and two subclasses from it, WorldMap and DungeonMap. When I compile the file, I get two errors, both the same thing.

      cannot find symbol
      symbol : constructor Map()

      The two lines with errors are marked out in the following code. They are the opening brace for WorldMap and the first line of DungeonMap's constructor.
      public class Map implements Serializable
      {
           protected int maxX = 0,
                maxY = 0;
           public int[][] elevation = null;
           public int[][] terrain = null;
      
           Map(int xSize, int ySize)
           {
                maxX = xSize - 1;
                maxY = ySize - 1;
      
                elevation = new int[xSize][ySize];
                terrain = new int[xSize][ySize];
           }
           ...
           a bunch of other methods down here
      }
      
      class WorldMap extends Map  // first error points to this line
      {
           ...
      }
      
      class DungeonMap extends Map
      {
           DungeonMap(int xSize, int ySize)
           {  // second error points to this line, just the single brace
                ...
      Thanks in advance for any help rendered, it's much appreciated.
        • 1. Re: Compiler complaining about superclass' constructor not present in subclass
          843785
          Because you didn't explicitly define a constructor for class WorldMap, the compiler is trying to generate the following one for you:
          class WorldMap extends Map  {
              WorldMap() {
                  super(); //<-- the problem
              }
          }
          ... but super class Map doesn't have a no-argument constructor, and that is the error message you get.

          Solution: either write a no-argument constructor for Map, or define an explicit constructor for WorldMap:
          class WorldMap extends Map  {
              WorldMap(int xSize, int ySize) {
                  super(xSize, ySize); //<-- the solution
              }
          }
          A similar solution holds for DungeonMap.

          Remember -- constructors are not inherited. I suspect you are assuming that they are...
          • 2. Re: Compiler complaining about superclass' constructor not present in subcl
            843785
            Thank you much; yes I was assuming that WorldMap would use Map's constructor.

            That fixed WorldMap, but it's still complaining about DungeonMap now. You said that what you explained applied to DungeonMap as well, but I did define a constructor for DungeonMap; in fact, it's the opening brace of that constructor that creates the error. What's up with that one?
            • 3. Re: Compiler complaining about superclass' constructor not present in subcl
              843785
              You need to explicitly call the super constructor, just as you did in WorldMap. It's the same problem.
              • 4. Re: Compiler complaining about superclass' constructor not present in subcl
                843785
                1. Constructors are not inherited into subclases.
                2. A class that has no constructors is given exactly one.
                A default, no argument costructor of public access.
                3. A constructor can call upon other constructors in the
                class for help, using the this() construct at the begin.,
                of the constructor.
                4. A constructor can call a specific constructor of the
                parent class by using the super() construct at the begin.,
                of the constructor.
                5. If you do not provide a call to either this() or super(),
                then Mr. Compiler creates a call to super() for you.
                6. If you extend a class that has no zero argument constructor
                or that constructor is not accessible in the sub class, then
                you can't allow the default call to super(). You must have an
                explicit call to a form of super() that you can access.
                • 5. Re: Compiler complaining about superclass' constructor not present in subcl
                  843785
                  filestream wrote:
                  2. A class that has no constructors is given exactly one.
                  A default, no argument costructor of public access.
                  The access level is the same as the access level of the class itself:
                  import java.lang.reflect.*;
                  
                  public class Outer {
                      static class Base {}
                  
                      public static class A extends Base{}
                      protected static class B extends Base{}
                      static class C extends Base{}
                      private static class D extends Base{}
                  
                      public static void main(String[] args) {
                          examine(Base.class);
                          examine(A.class);
                          examine(B.class);
                          examine(C.class);
                          examine(D.class);
                      }
                  
                      static void examine(Class<?> cls) {
                          for(Constructor<?> cons : cls.getDeclaredConstructors()) {
                              System.out.println(Modifier.toString(cons.getModifiers()) + " " + cls.getName());
                          }
                      }
                  }