Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA: override interface method

I have interface:

interface operations{  
    void sum();  
}   

and I want to have classes:

class matrix implements operations {  
    @override   
    void sum(matrix m) {  
    } 
}

class vector3D implements operations {  
    @override  
    void sum(vecor3D v) {  
    }
}

How to do this? I tried something like this:

interface operations < T > {  
    <T> void sum(T t);  
}

class matrix implements operations<matrix>{
    @Override
    void sum(matrix m){};
    }
}

class vector3D implements operations<vector3D>{
    @Override
    void sum(vector3D v){};
}

but it doesn't work.

like image 904
user3308470 Avatar asked Mar 20 '23 11:03

user3308470


1 Answers

Don't add a type parameters to the interface and the type. Also you should specify the generic parameters of the interface you implement:

interface operations<T> {  
    void sum(T t);  
}



class matrix implements operations<matrix> {  
    @Override   
    public void sum(matrix m){  
    } 
}



class vector3D implements operations<vecor3D> {  
    @Override  
    public void sum(vecor3D v){  
    }
}
like image 120
fabian Avatar answered Mar 29 '23 01:03

fabian