PL problems

Aliasing

Java type system allows subtyping through arrays which leads to the aliasing problem.

class A {
}

class B extends A {
  void bOnlyMethod() {}
}

class Main {
  public static void main(String args[]) { 
    B[] bArray = new B[10];
    A[] aArray = bArray;

    aArray[0] = new A(); // Java will crash at a runtime here
    bArray[0].bOnlyMethod();
    
    System.out.println("Done"); 
  } 
}

TypeScript allows this as well:

class A {
}

class B extends A {
    bOnlyMethod() {}
}

const bArray: B[] = [];
const aArray: A[] = bArray;

aArray[0] = new A();
bArray[0].bOnlyMethod(); // JavaScript will crash at a runtime here

console.log("Done");