The cause of java "incompatible types"?

import java.util.*;
class Poly{
    protected int[] coef = new int[1001];
    protected int[] exp  = new int[1001];
    protected int len;
    protected void attach(int c, int e){
        coef[len]  = c;
        exp[len++] = e;
    }
    protected void print() {
        for (int i=0; i<len; i++)
            System.out.print(coef[i]+" "+exp[i]+" ");
        System.out.println();
    }
}
class Add extends Poly{
    protected Add add(Add y) {
        int i,j;
        Add rel = new Add();
        for(i=0, j=0; i<len && j<y.len;) {
            if(exp[i] < y.exp[j]) {
                rel.attach(y.coef[j], y.exp[j]);
                j++;
            } else if(exp[i] > y.exp[j]) {
                rel.attach(coef[i], exp[i]);
                i++;
            } else{
                int c = coef[i] + y.coef[j];
                if(c != 0)
                    rel.attach(c,exp[i]);
                i++;
                j++;
            }
        }
        while(i < len)
            rel.attach(coef[i], exp[i++]);
        while (j < y.len)
            rel.attach(y.coef[j], y.exp[j++]);
        return rel;
    }
}
class Mul extends Add{
    protected Add mul(Add y){
        Mul sum = new Mul();     //the following error has been solved by change this
                                 //Mul to Add , why?
        for(int i=0;i<len;i++){
            Mul tmp = new Mul();
            for(int j=0; j<y.len; j++)
                tmp.attach(coef[i]*y.coef[j], exp[i]+y.exp[j]);          
            sum = sum.add(tmp);  //there are an error here
                                 //incompatible types
                                 //required : Mul
                                 //found : Add
        }
        return sum;
    }
}
public class answer{
    public static void main(String[] argv){
        Mul d = new Mul();
        Mul e = new Mul(); 
        d.attach(6,4);
        d.attach(7,2);
        d.attach(2,1);
        d.attach(8,0);       
        e.attach(9,5);
        e.attach(3,2);
        e.attach(-1,1);
        e.attach(5,0);       
        System.out.println("Mul");    
        System.out.println("D(x)= 9 5 3 2 -1 1 5 0");
        System.out.println("E(x)= 6 4 7 2 2 1 8 0"); 
        System.out.print("F(x)=");
        d.mul(e).print();
    }
}

This is a simple BigInt sample which I practice my extends concept,

There are an error in Class Mul:

  sum = sum.add(tmp);
         ^
 incompatible types
 required : Mul
 found : Add

I solved this problem when I change the Mul sum = new Mul(); toAdd sum = new Add ();` , in the same Mul class.

Why can it run? and why can't I use the original code Mul sum = new Mul() ?

the problem, I have searched in the Google always says that how to solved , but I want to know the concept.

Please, It's my first time to ask the question in English, Please be forgiving if you find anything offensive.

Thank you

Jon Skeet
people
quotationmark

The problem is that the add method is declared to return Add - but you're trying to assign the return value to a variable of type Mul. That's just not valid. If you change the declared type of sum to Add instead of mul, then that assignment is fine even if you initialize sum with a value of type Mul to start with.

It may be clearer to you if you separate out the method call from the assignment back to sum:

Mul sum = new Mul();
Mul tmp = new Mul();
...
Add result = sum.add(tmp);
sum = result; // This is the bit that's invalid

It's not really clear what you're trying to achieve here, but I'm pretty suspicious of a Mul class extending Add to start with...

people

See more on this question at Stackoverflow