Reputation: 1
My friend gave me this task and I am struggling with the last to, it asks me to:
"implement a class called Square that represents a square. Class Square must be derived from Rectangle. Make sure you override toString()."
I don't even think I am even close to getting it but any help would be great
public class Rectangle {
public double width;
public double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2*width+2*height;
}
@Override
public String toString() {
return "Rectangle[width "+width+",height "+height+"]Area:"+getArea()+",Perimeter:"+getPerimeter();
}
public static void main(String[] args) {
double width = (10);
double height = (10);
Rectangle rectangle = new Rectangle(width, height);
System.out.println(rectangle);
}
}
public class Sqaure extends Rectangle {
private final double width, height, area, perimeter;
public Sqaure(double width, double height) {
this.width = width;
this.height= height;
}
public static void main(String[] args) {
double width = (10);
double height = (10);
Sqaure sqaure = new Sqaure(width, height);
System.out.println(square);
}
}
Upvotes: 0
Views: 194
Reputation: 121
What your "friend" probably wants you to do is to use a super constructor. As it was already explained by Peter Lawrey, you do not need to define width, height in your extended class. It is considered a bad practice to hide fields in such a way. Square class will inherit those fields from its superclass Rectangle on the account of the extends keyword:
public class Sqaure extends Rectangle
This is how you might use a super constructor in your case.
public Sqaure(double side) {
super(side, side);
}
This will call the super class's constructor:
public Rectangle(double width, double height)
and by doing so, it will assign the Square's inherited fields width and height to the same value, making it a square. The inherited methods getArea() and getPerimeter() will still work, so there is no need to override them.
You will need to override the toString() method, because it has the word Rectangle in it. I am sure you can figure that one out yourself.
Upvotes: 0
Reputation: 18793
There is no need to overwrite more than the constructor and toString()
. As a square is just a special rectangle with equal side lengths, you can just initialize them to the same value, and all other methods will work as expected:
public class Square extends Rectangle {
public Square(double width) {
super(width, width);
}
@Override
public String toString() {
return "Square[width:" + width + "]Area:" + getArea() +
",Perimeter:" + getPerimeter();
}
Note that in the real world, you probably wouldn't use inheritance in this case, as this introduces some ambiguity -- you can still create squares with the rectangle constructor, and people may get mislead to use instanceof checks to determine whether a given rectangle is a square. Instead, one would probably add a single parameter constructor and check for width/height equivalence in toString.
Upvotes: 5
Reputation: 1123
You should note that sub and super classes do not need a main-class. You should have a separate main class where you initialize these objects.
To override a method use @Override
. You do this because you can initialize a subclass a super class, e.g., Rectangle square = new Square(...)
. When you call square.toString()
the output should not be Rectangle[width...]
, but Square[width...]
.
@Override
public String toString() {
return return "Square[width "+size+",height "+size+"]Area:"+getArea()+",Perimeter:"+getPerimeter();
}
If you do not override a method the functionality will be the same as of the super class (which is just fine for getArea()
and getPerimeter()
.
Please note: a Square
usually only has one parameter instead of two, because that is what makes it a Square
. ;)
Upvotes: 0