Object Oriented Programming

Apex is an Object Oriented Programming (OOP) Language, a programming paradigm where data is stored in objects and code in procedures (methods). The idea of OOP is to bind data and functions that operate on the data together to maintain access control through classes.

OOP brings quite a lot of programming concepts with it. Here are some concepts we will be using more often than others:

  • Inheritance: This allows you to 'inherit' methods from the parent class. This is similar to having kids. The kid could have green eyes from their mother and blonde hair from their father. We will discuss inheritance in detail later in this course because it can get really complicated and needs an entire day dedicated.
  • Encapsulation: This means the variables inside of a class are always hidden and can only be accessed using a method. An example would be, you cannot enter a room without first entering the building. The room is a variable, the floor a method, and the building a class.

Classes

Classes are templates to create objects that are an instance of the class and are useful for organizing related variables and functions. Think of classes like cookie cutters, they have a defined shape and structure and everything they operate on has a specific outcome (shape of the cookie). If we define a round cookie cutter as a class in our code we would say that it needs to have a circumference, a radius and an area. The values, however, can be calculated on the fly by just asking the user one value: the radius.

Here is the syntax for making a class:

scope modifier class ClassName{
}

Let's break this down:

  1. scope can be private, public or global.
  2. modifier can be virtual, abstract, with sharing, without sharing (optional)
  3. class declares that this variable is a class data type
  4. ClassName is the name of the variable. Note we do not follow camelCase when writing Class Names. This is to make a clear distinction between regular variables and classes.

Scope

Scope can be Private, Public or Global.

  • Private scope means any object outside the class cannot access methods inside of it
  • Public scope means any object throughout the application has access to it.
  • Global scope means any object throughout Apex has access to it.

Modifier

Sharing scope can be virtual, abstract, with sharing or without sharing.

  • virtual modifier allows you to override methods inherited from the parent class.
  • abstract modifier allows you to make methods without anybody defined.
  • with sharing modifier allows you to run the code in the context of the user's permission scope in Salesforce.
  • without sharing modifier allows you to run the code and access objects even outside of the user's permission scope in Salesforce.

To make a cookie cutter class, we define it the following way:

public class CookieCutter{
} 

Methods

Methods (also called functions when written outside the class) are template structures that take input parameters and return computed values. The syntax for Method is:

scope modifier return_dataType methodName(inputParameters){

return return_dataType
} 

Let's break it down

  1. scope can be public, private, protected or global.
  2. modifier can be override, static, final (optional).
  3. return_dataType defines what kind of value will the method return.
  4. methodName is the name given to this method. Follows camelCase.
  5. inputParameters are parameters that the method takes to process on them. (optional)
  6. return is a keyword that denotes a value is being returned

Scope

Scope can be Public, Private, Protected or Global.

  • Public scope makes the method available to code throughout the application.
  • Private scope makes the method inaccessible outside the class.
  • Protected scope is used when the method should only be accessible by its children classes.
  • Global scope makes the method available throughout Apex.

Modifier

  • override modifier is used when inheriting from parent class and rewriting parent method in the child class.
  • static modifier is used when creating a method/variable that is initialized only once and is associated with its outer class. There is no need for making a new instance of the class to access static methods.
  • final modifier is used when creating a method where the value cannot be changed once it's been inserted in the method/variable.
  • An example method would be:

    public String hello(String h){
    	String finalString = "Hello " + h;
    	return finalString;
    }

    To access this method, we would simply type the method name and our String in ( ) like so:

    hello('harshdeep');

    This would return;

    Hello harshdeep

    Now towards a more complex situation, the Cookie Cutter. To make a round cookie cutter, we would need a radius from the user, and return its circumference and area. Instead of writing this in our anon exec window, let's make a new file.

    In the developer console, click on new > apex class. Name the class CookieCutter and press enter.

    In the class, write three methods (refer to syntax guide if you have to):

    1. A private method named cir that returns Decimal and takes a Decimal as it's input. It returns the value 2 * 3.14 * r as a Decimal.
    2. A private method named area that returns Decimal and takes a Decimal as it's input. It returns the value 3.14 * r * r as a Decimal.
    3. A public method named radius that returns List<Decimal> and takes a Decimal as it's input. It adds the value from area and cir into the list and returns it.
    public class CookieCutter {
    
        private Decimal cir(Decimal r){
            Decimal fence = 2 * 3.14 * r;
    
            return fence;
        }
    
        private Decimal area(Decimal r){
            Decimal finalArea = 3.14 * r * r;
    
            return finalArea;
        }
    
        public List<Decimal> radius(Decimal r){
            List<Decimal> returnList = new List<Decimal>();
    
            returnList.add(area(r));
            returnList.add(cir(r));
    
            return returnList;
        }
    
    }

    Let's break this code down:

    • First, we make a public class called CookieCutter.
    • Out first method is cir, that returns a Decimal data type and takes a Decimal r as it's input parameter.
    • cir makes a new decimal called fence (short for circumference) and calculated the circumference of the circle using 2 pie r and returns the final value.
    • area makes a new decimal called finalArea that takes Decimal r as an input and calculates area using pie r square and returns the final value.
    • radius method takes decimal r as input and will return a list of decimal.
    • raidus method calls area and cir, adds them to the list and returns the final list.

    We will be running following code from Anon Exec Window (Control + E). Because this method is inside a class, we need to create a new instance of this class using the class name as a data type. This is the same as making a new variable, except the data type would be the class.

    CookieCutter finalCutter = new CookieCutter();

    This tells the computer to make a new template called finalCutter based on CookieCutter template. Now to access our radius method with 4.0 as it's radius, that returns a list of decimals, we call it like so:

    List<Decimal> finalList = finalCutter.radius(4.0);

    Now the list finalList has values of area and cir from inside the class! Go ahead and output it in the log and see the results!

    CookieCutter finalCutter = new CookieCutter();
    List<Decimal> finalList = finalCutter.radius(4.0);
    System.debug(finalList);

    You can have multiple methods with the same name inside the class, as long as there are ways to distinguish them.

    In our CookieCutter class, we can define another method that already exists but takes different inputs. Here’s some code:

    public class CookieCutter {
    
        private Decimal area(Decimal r){
            Decimal finalArea = 3.14 * r * r;
            return finalArea;
        }
    
        private Decimal area(Decimal r, String someVariable){
            return 2.0;
        }
    }

    Notice how we have two methods named area but both take different inputs! This is useful when your program makes use of optional parameters. An example would be when you need to generate numbers for your organization, but marketing department believes odd numbers are evil and must not be used. Your manager approves of this and asks you to write a new generator. This may or may not be from personal experience.

    Now that we are aware of what classes are, remember System.debug();? System is the name of the class and debug() is a method inside the class. Salesforce provides us with a bunch of inbuilt classes and methods like mail that reduce the amount of code we need to write. To know more about what classes ad methods are available to us, we need to read the documentation.

    Reading documentation can get confusing at times, but we do cover it at a later stage, with one day entirely dedicated to using documentation.

    Summary

    • Classes are templates that can be resued.
    • Methods are generic processing units that take input variables and return processedd results.
    • Cookies are delicious.
    Day 7: Week 1 Test