OOPs in Apex

Episode 2 – Introduction to Apex (Part 2)

In this episode we will pop the hood and dwell further to unravel the mysteries of programmatic development in Salesforce using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer.

Agenda

  • Anatomy of Class
  • Methods
  • Static Vs Instance methods
  • Pass by Value Vs Reference
  • Introduction to Object Oriented Programming (OOP)
    • Extending a Class
    • Interfaces
  • Q & A

Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Apex Hours for Student sessions will be recorded and will be available on our YouTube channel. Please Subscribe our YouTube Channel

Please register here and follow our ApexHours website to get more notification.

Anatomy of a Class

A Class is a user defined datatype (complex-type) and acts as a blueprint for their instances. 

An Object is an instance of a class i.e. a class in action.

 

Methods

• Reusable code snippets
• Specific purpose
• Knows what comes through
• Knows what goes out

Demo 1 – Class and Methods

public class Demo11{
public void printOutput(String stringToDisplay){
    System.debug('Display text: ' + stringToDisplay);
}
}

Static Vs Instance

Static Vs Instance
Static Vs Instance

Demo 2 – Static and Instance Variables & Methods

public class Demo2 {
	
	String helloWorldString;
	private static final String DEFAULT_STRING;
    
    static{
        DEFAULT_STRING = 'Hello World';
    }
		
	public Demo2(){
		this(DEFAULT_STRING);
	}
	
	public Demo2(String stringToDisplay){
		this.helloWorldString = stringToDisplay;
	}
	
	public static void printOutput(){
		System.debug('Display text: ' + this.helloWorldString);
	}
}

Demo2 d21 = new Demo2();
d21.printOutput();

Demo2 d22 = new Demo2(‘Jigar’);
d22.printOutput();

Pass by Value Vs Pass by Reference

Pass by Value Vs Pass by Reference

Demo3 – Pass by Value Vs Reference

public class Demo3 {
    
    public void mainValueMethod(){
        
        String websiteUrl = 'www.apexhours.com';
        
        System.debug('Before value call ' + websiteUrl);
        passByValueCall(websiteUrl);
        System.debug('After value call ' + websiteUrl);
    }
    
    private void passByValueCall(String websiteUrlValue){		//Pass by Value Call
        websiteUrlValue = 'www.salesforce.com';		
    }
    
    public void mainReferenceMethod(){
        
        Account a = new Account();
        a.Name = 'Test Account';
        a.Website = 'www.apexhours.com';
        
        System.debug('Before reference call ' + a);
        passByRefCall(a);
        System.debug('After reference call ' + a);
    }     

    private void passByRefCall(Account a){						//Pass by Reference Call
        a.Website = 'www.salesforce.com';
    }
}
	
	

“In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.”

NOTE: Primitive data types like Integer, Double are pass by value and Objects are pass by reference

Extending a Class

  • Use the virtual keyword
  • Mark the method and class as virtual
  • Extending class can override and provide a new definition to the virtual method

Interfaces

  • A contract that enforces what the child must do
  • Only contains method declarations not definitions
  • Cannot be instantiated on their own

Recording

Episode 2 will be presented by Jigar Shah on Feb 11, 2020 at 6 PM Indian Standard Time.

Further Learning

Apex Hours TrailMixes


Assignment

Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.

Write an Apex class MathCalculator that performs arithmetic operations on Integers based on the following methods on the input numbers passed to them as arguments.
add() – Sums 2 input integer arguments and returns the summation
multiply() – Multiplies 2 input integer arguments and returns the product

Add a method printOutput() that prints the summation or the product from the add or multiple methods to the Debug Log.

(Hint: Implement add(), multiply() and printOutput() as private static methods invoked by a public instance doMath() method. The doMath() method will be invoked from the Execute Anonymous window)

Don’t forget to register for our next session. Check this post for all other session detail.

Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

So, learn at your pace and free will and ace your journey to Salesforce!

34,111 total views, 117 views today

39 thoughts on “Episode 2 – Introduction to Apex (Part 2)”

  1. Solution :

    public class MathCalculator {

    private static integer add ( integer a, integer b ) {
    return a+b;
    }

    private static integer multiply ( integer a, integer b ) {
    return a*b;
    }

    private static integer subtract ( integer a, integer b ) {
    return a-b;
    }

    public MathCalculator() {
    }

    private static void printOutput ( integer output ) {
    system.debug(output);
    }

    public void doMath ( integer a, integer b, string operator ) {

    integer value = 0;

    if ( operator == ‘+’ . ) {
    value = add ( a , b );
    }

    else if ( operator == ‘*’ ) {
    value = multiply ( a, b ) ;
    }

    else if ( operator == ‘-‘ ) {
    value = subtract ( a, b ) ;
    }

    MathCalculator.printOutput ( value ) ;
    }

    }

    Reply
  2. Code for Assignment Day 2
    ——————————–

    public class MathCalculator {
    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }

    private static Integer subtract(Integer a, Integer b){
    return a-b;
    }

    private static void printOutput(Integer a, Integer b){
    System.debug(‘Summation of 2 values:’ + add(a,b));
    System.debug(‘Multiplication of 2 values:’ + multiply(a,b));
    System.debug(‘Substraction of 2 values:’ + subtract(a,b));
    }
    public void doMath(Integer a, Integer b){
    printOutput(a, b);
    }

    }

    In Developer Console –> Open Execute Anonymous Window
    ————————————————————————
    MathCalculator mathCalculator = new MathCalculator();
    mathCalculator.printOutput(5,3);

    Reply
  3. public class MathCalculator {
    private static integer add(integer a,integer b){
    return a+b;
    }
    private static integer multiply(integer a,integer b){
    return a*b;
    }
    private static void printOutput(integer a, integer b){
    System.debug(‘the summation of no is:=’+add(a,b));
    System.debug(‘the Multiplication of no is:=’+multiply(a,b));

    }
    public static void doMath(integer a, integer b){
    printOutput(a,b);
    }

    }

    calling a method:- MathCalculator.doMath(2,3);

    Reply
  4. Code for Assignment Day 2
    ——————————–

    public class MathCalculator {
    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }

    private static Integer subtract(Integer a, Integer b){
    return a-b;
    }

    private static void printOutput(Integer a, Integer b){
    System.debug(‘Summation of 2 values:’ + add(a,b));
    System.debug(‘Multiplication of 2 values:’ + multiply(a,b));
    System.debug(‘Substraction of 2 values:’ + subtract(a,b));
    }
    public void doMath(Integer a, Integer b){
    printOutput(a, b);
    }

    }

    In Developer Console –> Open Execute Anonymous Window
    ————————————————————————
    MathCalculator mathCalculator = new MathCalculator();
    mathCalculator.doMath(8,4);

    Reply

Leave a Comment