Inherited methods which return a base class instance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smani
    New Member
    • Nov 2011
    • 1

    #1

    Inherited methods which return a base class instance

    Hi,

    consider this inheritance of MyVector from MyPoint:

    Code:
    class MyPoint {
    /* ... */
        public MyPoint add(MyPoint other){ ... }
    }
    
    class MyVector extends MyPoint {
    /* ... */
    }
    
    /* somewhere in the code */
    MyVector v1 = new MyVector(...);
    MyVector v2 = new MyVector(...);
    
    MyVector v3 = v1.add(v2); // Error, cannot cast from MyPoint to MyVector
    I wonder whether I really must override all base class member functions just to change the return type, or is there a more elegant way to solve this problem?

    Thanks!
Working...