UPDATE: Based on various feedback, I'm significantly updating my question so much so that it is different from (though still related to) the original one. I don't know what the proper protocol is for this on StackOverflow, so I'm sorry if I'm doing the wrong thing.
I’m working in ServiceNow, a popular Software-as-a-Service platform implemented in Java that executes customer JavaScript code using the Rhino JavaScript engine. One of their APIs is GlideRecord, which is the API customers use to do CRUD operations against the MySQL database where all data is stored on the platform.
When I access a particular field value through the GlideRecord API, the field value is provided in the form of an object called GlideElement:
// JavaScript code
var gr = new GlideRecord('incident');
gr.setLimit(1);
gr.query();
gr.next();
var ge = gr.short_description; // GlideElement object
Through the magic of the Rhino engine, both GlideRecord and GlideElement are not JavaScript objects but Java objects somehow shared into the JavaScript runtime. The weird thing about the GlideElement object is it seems to somehow be an instance of java.lang.String:
gs.print(ge instanceof GlideElement); // true
gs.print(ge instanceof Packages.java.lang.String); // true
(The Packages object is supplied by Rhino as a way to access things like java.lang.String. This looks fishy, but as far as I know it is the real java.lang.String from Java, no funny business.)
My original question asked how something could be an instance of two classes and I now understand that's an ordinary thing enabled by polymorphism (subclassing and interface implementation). However, the java.lang.String class is a final class, so it shouldn't be subclassable or implementable by any other class.
Unfortunately the code above is the entirety of what I can provide because the SaaS platform's Java code is not open source, so I can't (and no one can) see how GlideRecord and GlideElement are implemented under the hood. Without access to the source code, what can we know or speculate about how this could have been implemented in either vanilla Java or some arcane witch magic enabled by the Rhino engine?
The code above can be executed if you have access to any ServiceNow instance. You can register for free and spin up a “personal developer instance” of ServiceNow at https://developer.servicenow.com. Once you have an instance you can navigate to All › System Definition › Scripts - Background to paste and execute arbitrary JavaScript code.
String,Object, and of the interfacesSerializable,CharSequence,Comparable,Constable,ConstantDesc(no idea aboutGlideElement) And I believe that we can add parents to classes/instances using code weaving like AspectJ or similarPackages.java.lang.String. I could imagine that that's not actually ajava.lang.Stringand is some repurposed thing that can do any hackery it wants?foreignObject instanceof Packages.java.lang.StringJavaScript code or Java code?