4

The Apex includes a system class named Object which serves as the foundation (superclass) for all standard and custom types.

Questions

  1. Which methods does the Object class contain, and which ones can be overridden?
  2. Is there any available documentation regarding the Object class? I found that: no (Source: Object class in Apex), but it was 2014.
  3. Is there something more than in Java Object?

My small investigation

toString()

public class A {}

System.debug(new A().toString()); // A:[]

public class A {
   public override String toString() {
       return 'Hello toString()';
   }
}

System.debug(new A().toString()); // 'Hello toString()'

equals()

public class A {}

System.debug(new A().equal('Some String'); // false

hashCode()

public class A {}

System.debug(new A().hashCode()); // 2069582068
System.debug('Some String'.hashCode()); // 2147069117

clone()

public class A {}

System.debug(new A().clone()); 
3
  • According to the placeholder created by Illuminated Cloud (I do not know how it is created), Object only has equals, hashCode and toString, and all three are virtual. It's possible that this placeholder has been fabricated by Scott (the dev for IC) or it may be queryable through the tooling API somehow - though on the latter I could not find it in the ApexClass table.
    – Phil W
    Jun 1, 2023 at 12:15
  • 2
    BTW, I feel that the lack of documentation is somewhat ridiculous.
    – Phil W
    Jun 1, 2023 at 12:19
  • NB: the previous Q&A you cite has a comment from April by Daniel Ballinger stating: "Object does not currently support a clone method. It only has toString(), equals(), and hashCode(). However, any Apex Class that you define will have an implicit clone method that you can override if you want to". Daniel is one of the PMs for Apex at Salesforce, so should know what he's talking about.
    – Phil W
    Jun 1, 2023 at 12:24

1 Answer 1

4

Which methods does the Object class contain, and which ones can be overridden?

equals, hashCode and toString.

Is there any available documentation regarding the Object class?

It has never been properly documented. I've asked if there is a reason why it cannot be.

Is there something more than in Java Object?

No.

Also...

Note that, as per a comment by Daniel Ballinger (PM for Apex at Salesforce):

any Apex Class that you define will have an implicit clone method that you can override if you want to.

I.e. clone is not on Object but the class implicitly receives this as part of its definition. This is a bit weird, but Apex isn't formally specified as such, at least not for people outside Salesforce.

The toString method is another weird one; while String supports it, for example, that isn't the case with Id. The important point here is that not all API classes actually extend Object, but all custom apex classes do.

1
  • Thank you Phil! Jun 1, 2023 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .