-2

I have this class:

public class Gradient extends Item<GradientRep> {

    /**
     * Initializes a new Gradient
     * @param x X-coordinate of the Item
     * @param y Y-coordinate of the Item
     * @param value The value of the gradient
     */

    public Gradient(int x, int y, int value, String type) {
        super(x, y);
        this.value = value;
        this.type = type;
    }

     ....

And I would like this class to "own" some objects of class Item, for example, I would like to retrieve from class Gradient this object:

Item MyItem = new Item()

Is there an obvious way to do this?

To give some background, for now the class Gradient only represent a fixed point on a map, which is not what we want for a Gradient. So, I want to propagate this gradient, and therefore when I instantiate a Gradient, I want it to provide other Items (with different coordinates on the map, which is dealt by the class Item).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ailauli69
  • 522
  • 6
  • 19
  • Did you try to add a variable of type Item to Gradient? I don’t really see what the problem is. – Joakim Danielson Apr 24 '21 at 20:00
  • Does this answer your question? [ extends Class> and super Class> in Java - why it works this way?](https://stackoverflow.com/questions/35669112/extends-class-and-super-class-in-java-why-it-works-this-way) – Monica Aspiras Labbao Apr 25 '21 at 06:58

2 Answers2

1

I'm not entirely sure what you're after - but you could instanciate items from Gradient like this:

import java.util.ArrayList;

public class Gradient extends Item<GradientRep> {

    private final int value;
    private final String type;
    
    private ArrayList<Item<GradientRep>> items = new ArrayList<>();

    /**
     * Initializes a new Gradient
     *
     * @param x     X-coordinate of the Item
     * @param y     Y-coordinate of the Item
     * @param value The value of the gradient
     */
    public Gradient(int x, int y, int value, String type) {
        super(x, y);
        this.value = value;
        this.type = type;
    }

    Item<GradientRep> getItem() {
        return new Item<>(x, y);
    }
}

The Gradient object will be an item, stores items in the items ArrayList (or your choice of ADT), and can generate new Item objects with the getItem method.

Tytrox
  • 483
  • 2
  • 10
  • Thank you, actually I don't need the Items to be of "type" GradientRep, Can I just do Item getItem() { ... etc. ? – ailauli69 Apr 24 '21 at 20:23
  • Sorry, I did not specified that the Item class is Abstract, and I would like to create Items objects in Gradient class, based on the values of x and y (for instance create an Item(x+1, y+2), and be able to access it once the Gradient class is instanciated) – ailauli69 Apr 24 '21 at 20:27
  • If the `Item` class is abstract, you can't instanciate `Item` objects. If you would like objects 'like' items, you will need to either make `Item` not abstract, or create another child of `Item`. Maybe something like: `public class Coordinate extends Item <> {` – Tytrox Apr 24 '21 at 20:40
  • And yes, you can have a raw use of `Item`, although I would avoid it if you could. – Tytrox Apr 24 '21 at 20:42
0

You can do it through this way as well:

public class Gradient extends Item<GradientRep> {
        Item myItem;
        /**
         * Initializes a new Gradient
         * @param x X-coordinate of the Item
         * @param y Y-coordinate of the Item
         * @param value The value of the gradient
         */
    
        public Gradient(int x, int y, int value, String type) {
            super(x, y);
            this.value = value;
            this.type = type;
            myItem = new Item();
        }

you can access the field as gradient.myItem

If you are making the field static, then use Gradient.myItem

prashant.kr.mod
  • 1,178
  • 13
  • 28