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);
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…
}
}
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.
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.
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.
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++.