COMP2396
Loading...
Searching...
No Matches
Shape.java
Go to the documentation of this file.
1// 2014 Dec q1c
2/* The error is because Java requires a call to the super class
3* constructor in the subclass constructor, if the
4* super class constructor is not default constructor.
5* THe fix would be adding
6* super(s, s); in the first line of the Square constructor
7* The output would be:
8* Rectangle()
9* Square()
10* Rectangle()
11 */
12
13class Rectangle {
14 double width, height;
15
16 public Rectangle(double w, double h) {
17 width = w;
18 height = h;
19 System.out.println("Rectangle()");
20 }
21}
22class Square extends Rectangle {
23 double size;
24 public Square(double s) {
25 // Requires a call to the super class constructor
26 super(s, s);
27 size = s;
28 System.out.println("Square()");
29 }
30}
31public class Shape {
32 public static void main(String[] args) {
33 Square sq = new Square(1.0);
34 Rectangle re = new Rectangle(1.0, 2.0);
35 }
36}
static void main(String[] args)
Definition Shape.java:32