Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:56:02+00:00 2026-06-17T22:56:02+00:00

I have an abstract super class that implements an interface: public abstract class FooMatrix

  • 0

I have an abstract super class that implements an interface:

public abstract class FooMatrix implements Matrix {
    public Vector multiply(Vector vec) {
        // Code for Matrix * Vector
    }
}

public interface Matrix {
    public Vector multiply(Vector vec);
}

and then I have a subclass that extends the super class and implements a second interface that represents a mathematical subclass of the mathematical class represented by the first interface:

public interface Vector {
    // Methods
}

public class FooVector extends FooMatrix implements Vector {
    @Override
    public Matrix multiply(Vector rightVec) {
        // Code for Vector * Vector
    }
}

So the subclass is returning a super class of the return type of the abstract super class. This isn’t working, and I want to know how I can make it work.

In mathematics, vectors are a subclass of matrices. A matrix times a matrix yields another matrix, in general. If the second matrix is a vector, the result is a vector. If both matrices are vectors, the result is a matrix or a scalar for column vector times row vector or row vector times column vector, respectively. This assumes the dimensions are compatible.

I would like to represent this behaviour with Java classes in a way that respects mathematics and gives the most specific return types possible. In other words, I want fooMatrix.multiply(Vector vec) to return Vector, and not just Matrix, but I want fooVector.multiply(Vector rightVec) to return Matrix and not Vector (which would be incorrect.) I can deal with a scalar as a 1×1 Matrix with another Vector method inner that calls multiply(Vector rightVec) and then returns the single element of the 1×1 return Matrix as a scalar.

I have found questions regarding covariant return types, but nothing like this.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:56:03+00:00Added an answer on June 17, 2026 at 10:56 pm

    Because Matrix and Vector are in two different hierarchies. You cant use Matrix in place of Vector as a return type.
    If you are not sure of the return type whether its going to be of type Matrix or Vector, you can new interface which is extended by Matrix and Vector and use it as return type.
    Somewhat like

    public interface MatrixOrVector {
        // This can be used as return type
    }   
    

    change Matrix to

    public interface Matrix extends MatrixOrVector{
       // Methods...
    }
    

    same with Vector

    public interface Vector extends MatrixOrVector{
        // Methods ....
    }
    

    If you use MatrixOrVector as return type, you can return object which is of type Matrix or Vector.

    Also just FYI FooMatrix is not a abstract class because you are missing abstract there. read more about abstract http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public class Foo { interface Coo<T> { public T
I have an abstract java class that implements a couple of its methods, but
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
I have a class that extends Activity like so: public abstract class AndroidGame extends
In my Android application, i have the following classes: public abstract class A implements
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
Suppose we have abstract class A (all examples in C#) public abstract class A
I have a abstract base C# class with a couple of methods that has
I have a generic class that is also a mapped super class that has
I have a class that has overloaded methods. public class MyCal extends GregorianCalendar {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.