Java Woes...

I have an assignment where I have to hava an abstract class Vec, which will contain the variables x and y as well as abstract methods for adding and subtracting 2 vectors. I then have to create classes Vec2D and Vec3D which extends Vec implements the two methods in it. For some reason Vec2D and Vec3D refuse to compile and say that I haven’t overriden addVec and subtractVec from the Vec Class.

Here’s the Vec class:

public abstract class Vec
{
protected double x;
protected double y;
public abstract Vec addVec(Vec V);
public abstract Vec subtractVec(Vec V);

}

and here’s a method in question:

   public Vec2D addVec(Vec2D V)
   {
      Vec2D temp;
      temp.x=this.x+V.x;
      temp.y=this.y+V.y;
      return temp;	      
   }

I know little of Java… But I’m good with C#, which people say, and I agree, is a son of the incestuous relation between father and daughter - C++ and Java.

In C#, you would write the Vec2D class like this, with the addition of the word “override”:

public class Ved2D : Vec // the ‘:’ is C#'s equivalent to Java’s “extends”
{
public override Vec2D addVec(Vec2D V)
{
// put the same logic here as above…
}
}

You don’t do that in Java… Methods are overriden just by copying the method header.

public Vec2D addVec(Vec2D V)

does not implement the method

public abstract Vec addVec(Vec V)

because in the second, the argument can be any Vec, while in the first it must be a Vec2D.

Hooray for teaching horrible design as early as possible. This is a poor way to design things. And yet your teacher has been apparently mislead into believing this is a legitimate design for implementing mathematical operations. How terrible.

What are the implementations of the methods going to do, access the variables declared in the abstract class? And how will the 3d version work, by adding a third variable? I refuse to believe this is a good design.

They access the variables from the abstract class and the 3D version does indeed add a third variable.

Hmm… I think I got it.

You’re not overriding the methods, you’re just overloading them because you’re adding a different method signature in the subclasses.

You’ll have to to it like this:

public Vec addVec(Vec V)
{
//logic
}

or

public Vec2D addVec(Vec V)
{
//logic
}

and work your way from there.

Either one should work. Again, I work with C#, not Java, so I don’t know which will do the trick for you.

hmmm, I guess I could make the methods use instanceOf to see if they are the correct type and then change the return and parameters to Vec. Seems a little assinine though.

Vorpy beat me to it >_<

Does Java have a typeOf too? It could help better than instanceOf if I’m thinking correctly.

Yes, you’ll need to use an instanceof statement. E.g.

if (V instanceof Vec2D)
{ //return a Vec2D
}
else return super.addVec(V); //return a regular Vec

Note that you can return a Vec2D just fine, because a Vec2D is a Vec, so returning one satisfies the return requirement.

It’s true that it’d be better design to overload the method rather than redefine it. However, if it is overloaded, it’s actually an excellent design because it allows for good extendibility and ease of use by taking advantage of polymorphism to mix and match vectors of differing dimensions. Of course, making it even more generic (e.g. by having an attribute for “number of dimensions”) would be better, but this is a good first start.

The problem is defining semantics for mixed vector lengths that make sense. I suppose you could just assume that if we have a vector of n dimensions and a bigger vector of m dimensions, the missing m-n values in the first vector are just 0. And then build some convoluted logic around that.

Or there’s always the simpler, easier to understand, safer approach of just making Vec2d incompatible with Vec3d. But what’s the fun in that? Let’s make things needlessly complicated, that’s what computers are for, apparently.

It works now. Java is fucking retarded and that’s all I have to say.

Both of you are missing the point here. This is obviously just an exercise in showing how to use inheritance. It’s not meant to be a full-fledged mathematical construct. It’s true that it’s not the absolute best way to go about it, but it does the job fairly well.

And actually, it makes perfect sense to add a 2D vector and a 3D vector by assuming that the 2D vector’s third dimension is zero. At least to me.

Java does take some getting used to, but it’s extremely powerful and (from my experience) tends to result in much more readable code than C/C++.

There, I fixed it for you.