5

Java inheritance works with "final" & without "virtual, override". Is there a specific reason for implementing it differently in apex?

2 Answers 2

5

The changes are related to security of the classes. In Java, the entire application is self-contained, and uses the modifiers private, protected, "friendly", and public. In Apex Code, applications co-exist with other applications, and have the modifiers private, protected, public, and global. A non-final global class could be extended to modify the behavior of that class, which is usually not the developer's intent, and in fact, could be considered a security breach, since it would allow an outside developer access to any data that can be returned by the global class, possibly including protected custom settings, encryption keys, etc.

It is for this reason that classes are final by default, and why you must explicitly grant additional subclassing permissions using virtual. On the flip side of the coin, requiring the "override" keyword makes it clear that the developer is altering the behavior of the parent class, and also prevents them from accidentally using the wrong function name or parameters, which would create a different function entirely that's not actually overriding the inherited function. The @Override annotation in Java protects you, but only if you actually use it. Without a habit of using it, you could easily write a non-functioning program and left scratching your head trying to figure out why.

0

A big difference between Java and Apex is that Apex is only designed to run in Salesforce's multi-tenant environment, so it generally has more constrained functionality driven by what Salesforce's customers have needed to accomplish in custom code.

The popularity of the AppExchange has also greatly increased the number of packaged classes on the platform, with their own limitations on inheritance and visibility.

The documentation offers occasional hints at some of the language design decisions: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_defining.htm

You must log in to answer this question.

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