Salesforce: Polymorphism driven by Apex class inheritance

Polywhatsthatnow? Polymorphism ” is the ability of one type, A, to appear as and be used like another type, B.

It is a facet of OOP and can best be described using the good ol’ example of the ‘Shape’ class problem (thanks Java!). Let’s suppose we’d like to create an application that drew a number of shapes on a page. At the time of analysis we know that we want to immediately support circles, but to future-proof our application we need to support the drawing of any shapes. Being Pretty Darn Good Developers(tm) we realise we should create a Shape class that has a single method used to draw itself, and any specific shapes should be derived from this class, and override that method. Make sense? Didn’t think so.

Read more

OOP in the Salesforce Cloud – ReCaptcha Revisited

Can I get a OOP OOP?! That’s right folks, OOP is alive and well on the Force.com platform, and over the past few months I’ve had some pretty thrilling experiences implementing projects using those trusted sidekicks Encapsulation, Polymorphism and trusty ol’ Abstraction. There is some pretty sweet documentation on the subject, but I thought I’d demo something supercool I learnt by combining two articles from the Force.com wiki.

Read more

Throwing Custom Apex Exceptions

Throwing custom exceptions is something that most Java developers expect to be able to do when working with Apex code, and indeed it is possible. It isn’t very well documented, but I managed to stumble across this solution after some experimentation. Usually, I would expect an exception to be thrown using syntax at least similar to: However this syntax will give the follow error: Type cannot be constructed: Exception Instead you have to create a new exception class with extends the Apex Exception class: And then throw that new exception: That’s just the beginning of course, you could create some pretty fancypants error handling in your custom exceptions. The world is your oyster, now all your need is some Tabasco and lime.