instanceof in Apex If we need to verify at runtime whether an object is actually an instance of a particular class use instanceof keyword. The instanceof keyword can only be used to verify if the target type in the expression on the right of the keyword is a viable alternative for the declared type of the expression on the left. You could have something which is passed as Object type and using instanceof you can determine if it is an instance of your class or other data types below code will help understand the use instanceof keyword. Open developer console and run below code and observe the logs InstanceofController.getType(new Account()); INstanceofController.getType(true); INstanceofController.getType(Id.valueOf('001xa000003DIlo')); InstanceofController.getType(Date.newInstance(2020, 12, 31)); InstanceofController.getType(DateTime.newInstance(2020, 12, 31,0,0,0)); InstanceofController.getType('Clever Fox'); InstanceofController.getType(Blob.valueo...
Posts
Showing posts with the label instanceof apex
- Get link
- X
- Other Apps
By
Puneet Mishra
-
Virtual Class in Apex A class that extends another class inherits all the methods and properties of the extended class. In addition extending class can override the existing virtual methods by using override keyword in the method definition. Overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behavior of a particular method is different based on the object you're calling it. This is referred as polymorphism . just like abstract class, it is not mandatory to override virtual methods. virtual and abstract classes can extends virtual class. Check below code Now open developer console and run below code snippet and observe the logs Marker obj1, obj2; obj1 = new Marker(); // This outputs 'MARKER: writing some text' obj1.write(); obj2 = new BlueMarker(); // This outputs 'BLUEMARKER: writing some text using BlueMarker' obj2.write(); // We get the price of marker // and can call it fr...