1

Here's the code

class TwoD {
    int x, y;
    public TwoD(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
}


class ThreeD extends TwoD {
    int z;
    public ThreeD(int x, int y, int z) {
        super(x, y);
        this.z = z;
    }
}


class FourD extends ThreeD {
    int t;
    public FourD(int x, int y, int z, int t) {
        super(x, y, z);
        this.t = t;
    }
}



 class coords<T extends TwoD> {
    T cordinates;
    public coords(T cordinates) {
        super();
        this.cordinates = cordinates;
    }
    static void show(coords<? super ThreeD> c) {}
  }


public class mainX {
    public static void main(String a[]) {
        FourD fourD = new FourD(1, 2,3,4);
        coords check = new coords(fourD);
        coords.show(check);

        TwoD twoD = new TwoD(1, 2);
        coords check1 = new coords(twoD);
        coords.show(check1);

      // How this program runs fine with the child and parent subclass objects in show method?

    }
}

The method

static void show(coords c)

should only allow Parent class objects ? Why is it allowing child class objects also? How this program runs fine with the child and parent subhclass objects in show method?

I am confused!

krishna
  • 63
  • 1
  • 6
  • Possible duplicate of [Difference between super T> and extends T> in Java](https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java) – Udith Gunaratna Sep 18 '19 at 12:50
  • @UdithGunaratna Sorry but NO :) – krishna Sep 18 '19 at 12:53
  • 2
    You're using raw types here (`coords` instead of `coords` and `coords`) so the type checks are disabled (see https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Thomas Sep 18 '19 at 12:54
  • @Thomas Thanks for the answer but why with raw types there was no error? The code was successfully executed! – krishna Sep 18 '19 at 14:53
  • Well your code doesn't seem to do anything with the parameter and even if it would do something it might still run (unless you're trying to access things that aren't present in the object you've passed). – Thomas Sep 18 '19 at 14:56
  • THANKS A LOT @Thomas – krishna Sep 18 '19 at 15:36
  • Well, you should read/learn a little more about `super` and `extends` (start here: https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super) and do some tests. `? super ThreeD` would mean that the generic type could be `ThreeD`, `TwoD` or `Object`. However, I can't tell why only that `x` is accessible because I don't know what `CGen`, `Dimension` etc. actually are - that `x` might not be what you think it is. – Thomas Sep 19 '19 at 07:21

1 Answers1

1

As mentioned by @Thomas in the comments, you're using raw types for your coords. (I could go into detail, but the linked answer explains everything very clearly. Your use case is mainly mentioned in the sections How's a raw type different from using <?> as a type parameter? and A raw type is the erasure of that type.)

If you'd change:

coords check = new coords(fourD);
...
coords check1 = new coords(twoD);

To:

coords<FourD> check = new coords<>(fourD);
...
coords<TwoD> check1 = new coords<>(twoD);

You would get the error you'd expect:

error: incompatible types: coords<TwoD> cannot be converted to coords<? extends ThreeD>
  coords.show(check1);
              ^

PS/off-topic: Class coords should be with a capital C (thus Coords) when following Java's code standards.

Kevin Cruijssen
  • 8,426
  • 8
  • 49
  • 114
  • Thanks for the answer but why with raw types there was no error? The code was successfully executed! – krishna Sep 18 '19 at 14:53