Inheritance


Compilation Errors


Error Message Name:

	Error: cannot find symbol
	symbol: constructor {class name}

Example: Suppose we have the Test class that extends the Board class:

	public class Test extends Board
	{ public Test(int row, int col)
   	  { System.out.println("Your board will have " + row + "rows");
     	    super(row, col);
          }
	}

We will have the following error message when compiling the program:

	Error: cannot find symbol
	symbol: constructor Board()

Cause: The first line in the constructor of the derived class must be a call to the superclass’ constructor, e.g.,

	super(parameters)

Solution: We can simply flip the order of the statements, so that super(parameters) becomes the first line in the constructor:

	public class Test extends Board
	{ public Test(int row, int col)
   	  {  super(row, col);
             System.out.println("Your board will have " + row + "rows");
          }
	}

Error Message Name:

	Error: call to super must be the first statement in the constructor

Example: Suppose we want to write a new putPeg method that not only puts a peg onto a board, but also prints out what row it will be on:

	import java.awt.*;
	public class Test extends Board
	{ public Test(int row, int col)
   	  { super(row, col);
   	  }
   
  	  public void putPeg(Color color, int row, int col)
  	  { super(row, col);
            System.out.println("We have put a peg at row " + row );
          }
        }

This would generate the following error message:

 	Error: call to super must be first statement in constructor

Cause: We are trying to call the superclass’ putPeg method. Recall, to invoke a method in the super class, the syntax is super.{method name}(parameters list). However, we have only super{parameter list}. Consequently, Java thinks that we are trying to call the constructor of the superclass.

Solution: A solution of the above example would be:

	import java.awt.*;
	public class Test extends Board
	{ public Test(int row, int col)
   	  { super(row, col);
          }
   
  	  public void putPeg(Color color, int row, int col)
  	  { super.putPeg(row, col);
    	    System.out.println("We have put a peg at row " + row );
  	  }
	}