Outter and Inner Classes
Not Started

A class is either an Outer class (also called Top-Level) or an Inner class.

Outer classes are defined within Apex files that share the same name as the class. If you’re looking at the Developer Console, the filename will have an .apxc extension. If you’re in VSCode, you’ll see a .cls extension.

The example in the last lesson was an outer class:

public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; }

This class would be defined in a file named Employee.cls or Employee.apxc, depending on where you’re looking. We’ll just use the .cls extension from here on out.

An Inner class will not have its own file. It’s a subclass that’s defined inside of an Outer class. Instead of declaring Employee as an Outer class, we could instead declare an Outer class named Company and have Employee as an Inner class:

public class Company { String companyName; public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; } }

With this structure, we’ve created a dependency between Employee and Company. An Employee cannot exist without a Company. This would all be defined in a file named Company.cls.

The opening and closing braces { and } define the scope. They indicate that Company starts on the first line and ends on the last line. Employee starts on the 5th line and ends on the 2nd to last line.

Another important keyword here is public preceding the class keyword. public is an access modifier. public classes can be accessed by any class in the same namespace. Inner classes have an implicit private access modifier by default, but can be updated. private classes can only be accessed by their outer class. Here’s the full list of access modifiers from Salesforce.

You may have seen keywords like with sharing, without sharing, or inherited sharing on class declarations. These keywords are used to dictate whether or not the class should enforce sharing rules on sObject data. Once we talk about manipulating sObjects in Apex - we’ll cover this topic in depth.

You may have also seen keywords like abstract, virtual, extends, and implements used in class definitions. These topics will be covered in the advanced course. We believe it’s important to master the fundamentals first.

Challenge

Create a new class named School. Declare the following String member variables: city, country, schoolName.