cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 159 forks source link

Wrong field access when inherited class have same field name as super class. #690

Open deadlocklogic opened 3 years ago

deadlocklogic commented 3 years ago

Tested with the live sandbox (version 3.0.0):

package org.jsweet;

import static def.dom.Globals.*;

public class HelloWorld {
    static class Class1 {
        int val = 1;
    }

    static class Class2 extends Class1 {
        int val = 2;
    }

    public static void main(String[] args) {
        System.out.println(((Class1) new Class2()).val);
    }
}

Expected result: 1 Actual result: 2

Why there is no true inheritance level mechanism implemented? Because doing few tests on typecasting looks like they are ignored anyway. But when same field name is used in many inheritance levels this approach fails therefore each created object should hold an object of inherited class, and when casted invoke that object for proper typecasting.