Skip to main content

Determining an Object Type

Determining an Object Type

Given an object, the %RegisteredObjectOpens in a new tab class provides methods to determine its inheritance. This section discusses them.

%Extends()

To check if an object inherits from a specific superclass, call its %Extends() method, and pass the name of that superclass as the argument. If this method returns 1, then the instance inherits from that class. If it returns 0, the instance does not inherit from that class. For example:

SAMPLES>set person=##class(Sample.Person).%New()
 
SAMPLES>w person.%Extends("%RegisteredObject")
1
SAMPLES>w person.%Extends("Sample.Person")
1
SAMPLES>w person.%Extends("Sample.Employee")
0

%IsA()

To check if an object has a specific class as its primary superclass, call its %IsA() method, and pass the name of that superclass as the argument. If this method returns 1, the object does have the given class as its primary superclass.

%ClassName() and the Most Specific Type Class (MSTC)

Although an object may be an instance of more than one class, it always has a most specific type class (MSTC). A class is said to be the most specific type of an object when that object is an instance of that class and is not an instance of any subclass of that class.

For example, in the case of the GradStudent class inheriting from the Student class that inherits from the Person class, for instances created by the commands:

 set MyInstance1 = ##class(MyPackage.Student).%New()
 set MyInstance2 = ##class(MyPackage.GradStudent).%New()

MyInstance1 has Student as its MSTC, since it is an instance of both Person and Student, but not of GradStudent. MyInstance2 has GradStudent as its MSTC, since it is an instance of GradStudent, Student, and Person.

The following rules also apply regarding the MSTC of an object:

  • The MSTC of an object is based solely on primary inheritance.

  • A non-instantiable class cannot ever be the MSTC of an object. An object class is non-instantiable if it is abstract.

To determine the MSTC of an object, use the %ClassName() method, which is inherited from %RegisteredObjectOpens in a new tab

classmethod %ClassName(fullname As %Boolean) as %String

Where fullname is a boolean argument where 1 specifies that the method return a package name and class name and 0 (the default) specifies that the method return only the class name.

For example:

 write myinstance.%ClassName(1)

(Similarly, you can use %PackageName() to get just the name of the package.)

FeedbackOpens in a new tab