You use the convert function to convert a Java object of one class to a Java object of a superclass or subclass of that class.
As shown in “Subtypes in Java-SQL data”, the home_addr column of the emps table can contain values of both the Address class and the Address2Line class. In this example:
select name, home_addr>>street, convert(Address2Line, home_addr)>>line2, home_addr>>zip from emps
the expression “convert(Address2Line, home_addr)” contains a datatype (Address2Line) and an expression (home_addr). At compile-time, the expression (home_addr) must be a subtype or supertype of the class (Address2Line). At runtime, the action of this convert invocation depends on whether the runtime type of the expression’s value is a class, subclass, or superclass:
If the runtime value of the expression (home_addr) is the specified class (Address2Line) or one of its subclasses, the value of the expression is returned, with the specified datatype (Address2Line).
If the runtime value of the expression (home_addr) is a superclass of the specified class (Address), then a null is returned.
Adaptive Server evaluates the select statement for each row of the result. For each row:
If the value of the home_addr column is an Address2Line, then convert returns that value, and the field reference extracts the line2 field. If convert returns null, then the field reference itself is null.
When a convert returns null, then the field reference itself evaluates to null.
Hence, the results of the select shows the line2 value for those rows whose home_addr column is an Address2Line and a null for those rows whose home_addr column is an Address. As described in “The treatment of nulls in Java-SQL data”, the select also shows a null line2 value for those rows in which the home_addr column is null.