Inheritance in BlueJ, parent class has two parameters but child class has one...
I've been given an assignment that involves inheritance on BlueJ.
The superclass/ parent class has the following line of code:
"
abstract void changeSize(int newHeight, int newWidth);
"
The classes I am dealing with represent different shapes, where the changeSize methods are defined by two parameters for the size, whereas some are only defined by one parameter.
My task is to correct this problem, but I have no idea where to start.
Any help is greatly appreciated :o
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Quote:
whereas some are only defined by one parameter.
Sounds like you need to give a default value for one the argument you pass to the super's method.
Can you make a small program to show the problem.
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Yeah sure :)
(Superclass)
public abstract class Shapes
{
abstract void changeSize(int newHeight, int newWidth);
}
(Sub-class 1)
public class Triangle extends Shapes
{
private int height;
private int width;
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
}
(Sub-class 2)
public class Square extends Shapes
{
private int size;
public void changeSize(int newSize)
{
erase();
size = newSize;
draw();
}
}
So as you can see, in sub-class 1 where it represent a triangle, there are two values which are assigned to 'newHeight' and 'newWidth' from the superclass. In sub-class 2, however, it only requires one value.
Hope this is what you asked for, thanks again :)
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Given that definition of Shape and that Square must extend Shape, you'll have to implement the Shape changeSize(..) method that takes 2 parameters in Square. In that method, check that both parameters are the same (i.e. height equals width), then call the single argument changeSize method passing either the height or the width. If the two parameters are not the same, an exception should be thrown. If you haven't covered exceptions yet, just decide which argument will specify the square size, pass it to the single argument method, and put a comment on the method explaining this (e.g. explain that argument 2 is not used).
[It's worth considering whether it really makes sense to have a changeSize(..) method in Shape that takes 2 arguments, because you'll have the same problem with Circle... Often with this kind of thing there's no 'correct' answer. The best answer will generally depend on exactly how you're going to use the objects].
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Quote:
Originally Posted by
dlorde
you'll have to implement the Shape changeSize(..) method that takes 2 parameters in Square. In that method, check that both parameters are the same (i.e. height equals width), then call the single argument changeSize method passing either the height or the width.
Personally I would do it the other way around. The single parameter method calls the 2 parameter method.
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Quote:
Originally Posted by
Junky
Personally I would do it the other way around. The single parameter method calls the 2 parameter method.
Fair enough; I'm not sure it matters - it's not really like cascading constructors; when dealing with a list of Shapes, the 2 parameter method will be called, and I prefer to see the more general method calling the more specific method. I guess I think of it in terms of an existing Square class that implements the Shape interface, so the interface implementation delegates to the Square-specific method...
Also because, for a Circle implementation, it would seem odd to have the single argument method call the 2 argument method which would then ignore one argument; and it makes sense to keep a consistent strategy - the Shape interface as a kind of Adapter pattern.
But it's way past my bedtime, and I may be missing something really obvious ;)
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Thank you for the help, got the assignment done thanks to you, and good to see you're from Faversham dlorde, as I'm only in Whitstable :)
Re: Inheritance in BlueJ, parent class has two parameters but child class has one...
Quote:
Originally Posted by
rph
Thank you for the help, got the assignment done thanks to you, and good to see you're from Faversham dlorde, as I'm only in Whitstable :)
Howdy neighbor! Glad you got it done <):)