0

I'm a student working on a project in Java where I need to practice using inheritance by extending the default Rectangle class; the child class MyRectangle needs to have constructors that accept parameters that describe the rectangle's border and fill colors as well as its dimensions. I also need to include getter/setter methods for the border/color attributes, a method that will rotate a rectangle by 180 degrees, and methods to calculate and return the area and perimeter of a rectangle. All of the required methods/fields are described in greater detail in the documentation file:///C:/Users/sweis/Downloads/MyRectangle.html here, and the output should end up looking like this: https://i.stack.imgur.com/KP7LB.png

I've written some code, but I've gotten a lot of error messages referencing the portion of the program where I actually define the MyRectangle class and set up constructors, including "Non-static variable cannot be referenced from a static context" and "error: cannot find symbol". I'm pretty new at Java so I'm really not sure, but I'm afraid I'm fundamentally misunderstanding something about inheritance, and I'm struggling to resolve these errors on my own. I would really appreciate it if someone could look at what I've written so far and help me understand what I'm doing wrong so I can better understand this concept. Here are the portions that are causing problems:

import java.awt.Rectangle;
class Main {
  public static void main(String[] args) {
    MyRectangle mr1 = new MyRectangle(0, 0, 4, 2, "blue", "red", true);
    MyRectangle mr2 = new MyRectangle(5, 5, 10, 3, "green");
    MyRectangle mr3 = new MyRectangle();

    System.out.println(mr1 + " " + "area = " + mr1.area() + " perimeter = " + mr1.perimeter());
    System.out.println(mr2 + " " + "area = " + mr2.area() + " perimeter = " + mr2.perimeter());
    System.out.println(mr3 + " " + "area = " + mr3.area() + " perimeter = " + mr3.perimeter());
    System.out.println(mr4 + " " + "area = " + mr4.area() + " perimeter = " + mr4.perimeter());
  }

  public static class MyRectangle extends Rectangle {
    public static int width, height;
    public String color, borderColor;
    public boolean border;

    public MyRectangle() {
      super();
    }

    public MyRectangle(int x, int y, int w, int h, String c) {
      width = w;
      height = h;
      color = c;
    }

    public MyRectangle(int x, int y, int w, int h, String c, String bc, boolean b) {
      borderColor = bc;
      border = b;
    }
    

    public int area() {
      return (width * height);
    }

    public int perimeter() {
      return ((width * 2) + (height * 2));
    }

    public void setBorder(boolean newBorder) {
      border = newBorder;
    }

    }
  }
}

If you have any questions or need me to clarify anything I'll definitely do so. Thank you!

deepakchethan
  • 5,240
  • 1
  • 23
  • 33
  • 1
    MyRectangle object `mr4` is not created but used in the print statements, this would be resulting in `cannot find symbol` – deepakchethan Jun 06 '21 at 23:40
  • Does this answer your question? [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Alessandro Baffa Jun 06 '21 at 23:43
  • `width` and `height` are also properties of `MyRectangle` *Objects* and not the *Class*, so you should drop the `static` modifier on those. Also why do you have `int x` and `int y` in your Constructors? You aren't using those anywhere. Else it does not look like an Error caused by falsly using (non-)`static`, atleast in the code snippet that you provided. – Quadslab Jun 07 '21 at 00:11
  • Welcome to StackOverflow! Your question looks like you put some effort into asking, which is highly appreciated here. I'd still like to point out a the following: When you get an error, it makes sense to include the whole error message, including the line number and the line of code that is usually given, so others can find the location they have to look at more quickly. [Note that it is discouraged to upload images of text here](//meta.stackoverflow.com/q/285551). Instead, this should simply be pasted into the question, with proper formatting. – He3lixxx Jun 07 '21 at 13:04

1 Answers1

3

The issue not really with the inheritance here.

Non-static variable cannot be referenced from a static context

As main method is static, it can't access inner non-static class MyRectangle inside it. You have already added static keyword to your MyRectangle class definition so this should fix this issue. You call also move MyRectangle class outside your main class. This would automatically make it static as all "Top level classes are static by default".

There is another issue here, the variables width, height from MyRectangle class are defined as static. This would result in these variables shared across all the different MyRectangle objects that you have created and override each others values. So remove the static keyword from there as they need to be different for each instance.

error: cannot find symbol

Object mr4 is not created but used in the print statements, this would be resulting in cannot find symbol. So just creating this object should fix your issue.

After these fixes your code should look something like:

class Main {
  public static void main(String[] args) {
    MyRectangle mr1 = new MyRectangle(0, 0, 4, 2, "blue", "red", true);
    MyRectangle mr2 = new MyRectangle(5, 5, 10, 3, "green");
    MyRectangle mr3 = new MyRectangle();
    MyRectangle mr4 = new MyRectangle(); // now mr4 is created 

    System.out.println(mr1 + " " + "area = " + mr1.area() + " perimeter = " + mr1.perimeter());
    System.out.println(mr2 + " " + "area = " + mr2.area() + " perimeter = " + mr2.perimeter());
    System.out.println(mr3 + " " + "area = " + mr3.area() + " perimeter = " + mr3.perimeter());
    System.out.println(mr4 + " " + "area = " + mr4.area() + " perimeter = " + mr4.perimeter());
  }

  static class MyRectangle extends Rectangle {
    public int width, height; // this should be non static
    public String color, borderColor;
    public boolean border;

    public MyRectangle() {
      super();
    }

    public MyRectangle(int x, int y, int w, int h, String c) {
      width = w;
      height = h;
      color = c;
    }

    public MyRectangle(int x, int y, int w, int h, String c, String bc, boolean b) {
      this(x, y, w, h, c); // this should initialise other args by calling different constructor
      borderColor = bc;
      border = b;
    }


    public int area() {
      return (width * height);
    }

    public int perimeter() {
      return ((width * 2) + (height * 2));
    }

    public void setBorder(boolean newBorder) {
      border = newBorder;
    }
  }
}
deepakchethan
  • 5,240
  • 1
  • 23
  • 33