5

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.

3
  • Read up about generic methods and how they interact with generic classes. Commented May 27, 2014 at 23:50
  • when implementing operations you must specify which is the actual type i.e. operations<matrix> Commented May 27, 2014 at 23:53
  • @Override has an upper-case O. If you use a lower-case o it will be useless. (Or more likely won't compile.) Commented May 27, 2014 at 23:59

1 Answer 1

7

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){  
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I also noticed missing 'public' before methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.