
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

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

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
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!
Comments(52)
Ankit Desai says:
February 11, 2020 at 2:54 pmCompleted Assignment for Day 2
Amit Chaudhary says:
February 12, 2020 at 2:38 am11 more to go
Manish Upadhyay says:
February 11, 2020 at 4:24 pmCompleted Assignment for Day 2
Akhil Kulshrestha says:
February 11, 2020 at 10:08 pmThe assignment is complete
Amit Chaudhary says:
February 12, 2020 at 2:38 am11 more to go
Trusha Shah says:
February 12, 2020 at 2:25 amDay 2 : Introduction to Apex
Assignment is completed.
Amit Chaudhary says:
February 12, 2020 at 2:38 am11 more to go
Marishell Cruz says:
February 12, 2020 at 3:05 amDone with Assignment 2
kiran says:
March 20, 2020 at 7:18 amcan you send a Solution ?
Aathirai says:
February 12, 2020 at 4:38 amCompleted Day 2 assignment
Adi says:
February 12, 2020 at 4:46 amWaiting for the recording.
A Belano says:
February 12, 2020 at 5:55 amDay 2 assignment completed
Anuj Sahu says:
February 12, 2020 at 6:36 amCompleted Assignment for Day 2
Archit Dutt Sharma says:
February 12, 2020 at 6:58 amCompleted Assignment for Day 2
Diksha Chhabra says:
February 12, 2020 at 7:38 amIntroduction to Apex (Part 2) : Assignment completed.
Rishab Goyal says:
February 12, 2020 at 7:40 amAssignment completed -2
Fanele Ndzoko says:
February 12, 2020 at 10:21 amAssignment 2 done.
Please upload the video.
Keerthana Ramesh says:
February 13, 2020 at 8:22 amDay 2 Assignment Completed.
Brisilda says:
February 13, 2020 at 12:08 pmEpisode 2 – Introduction to Apex (Part 2) Assignment is completed 🙂
Michael Wind says:
February 13, 2020 at 1:18 pmcomplete assigment day 2
kiran says:
March 20, 2020 at 7:19 amcan you send a Solution ?
sayanta says:
February 13, 2020 at 1:20 pmcompleted assignment
Isaac Arcos Huicochea says:
February 13, 2020 at 3:19 pmAssignment 2 Completed
Isaac Arcos Huicochea says:
February 13, 2020 at 3:20 pmAssignment 2 has been Completed
Chetan Sharma says:
February 13, 2020 at 8:33 pmCompleted Assignment 2
Arjinder Kaur says:
February 15, 2020 at 2:51 amDay2 – Assignment done
otieno says:
February 15, 2020 at 12:28 pmDay 2 Assignment: Completed.
Thanks for the the awesome session
Raman Chopra says:
February 16, 2020 at 3:53 pmCompleted Assignment for Day 2
Titli Banerjee says:
February 16, 2020 at 4:18 pmDay2 assignment complete.
Jaya Iyer says:
February 18, 2020 at 3:43 amcompleted assignment 2
James Fody says:
February 18, 2020 at 6:52 pmAssignment 2 completed
Pritam Pramod Dalvi says:
February 26, 2020 at 11:40 amAssignment 2 Completed
NARESH CHANDA says:
March 12, 2020 at 5:51 amDay 2 Assignment Completed.
ASHOK PANIGRAHI says:
March 17, 2020 at 4:58 pmDay 2 Assignment Completed.
ASHOK KUMAR PANIGRAHI says:
March 17, 2020 at 5:16 pm#ApexHoursDev @ApexHours
Day2 Assignment Completed
Arjinder Kaur says:
April 7, 2020 at 2:21 amSolution :
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 ) ;
}
}
Ankit says:
September 20, 2021 at 4:33 amHi Arjinder, This is Ankit , I have started with Salesforce recently. Could I request you to Help me with the Above code if you can take out 5 mins from your schedule. I’ll appreciate that!!!
Ankit Desai says:
April 10, 2020 at 8:50 pmCode 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);
muram nagarjuna reddy says:
April 18, 2021 at 6:45 amLine: 2, Column: 16
Method does not exist or incorrect signature: void printoutput(Integer, Integer) from the type mathcalculator
Sandya says:
May 31, 2021 at 6:16 ampublic class MathCalculator {
private static integer add(Integer a, Integer b){
return a+b;
}
private static integer multiply(Integer a, Integer b){
return a*b;
}
public static void printOutput(Integer a, Integer b){
system.debug(‘add’+ add(a,b));
system.debug(‘multiply:’ + multiply(a,b));
}
public static void doMath (Integer a, Integer b){
printOutput(a,b);
}
}
mathCalculator.printOutput(5,3);
prateek says:
July 26, 2020 at 8:45 ampublic 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);
Aman Kumar Mishra says:
September 10, 2020 at 8:49 pmCode 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);
Nuthan says:
January 2, 2021 at 5:48 pmHow the memory allocated for static methods since instance is not required to allocate memory
Eldhose says:
May 5, 2021 at 10:59 pmpublic class MathCalculator {
Private static Integer add(Integer num1, Integer num2){
return num1 + num2;
}
Private static Integer multiply(Integer num1, Integer num2){
return num1 * num2;
}
Private static void printOutput(Integer result){
System.debug(‘Result: ‘ + result);
}
Public void doMath(String operation, Integer num1, Integer num2){
if(operation == ‘Add’)
MathCalculator.printOutput(MathCalculator.add(num1, num2));
else if(operation == ‘Multiply’)
MathCalculator.printOutput(MathCalculator.multiply(num1, num2));
}
}
/*
MathCalculator mc = new MathCalculator();
mc.doMath(‘Add’, 10, 15);
mc.doMath(‘Multiply’, 4, 20);
*/
Amit Chaudhary says:
May 19, 2021 at 10:30 amAll the best for your future assignment
Md Arif khan says:
May 16, 2021 at 3:51 ampublic class MathCalculator{
private static Integer multiply(Integer num1, Integer num2){
return(num1*num2);
}
private static Integer add(Integer num1, Integer num2){
return(num1+num2);
}
private static void printOutput(Integer result){
system.debug(‘Result: ‘+result);
}
public void doMath(String operation, Integer num1, Integer num2){
if(operation==’Add’)
MathCalculator.printOutput(MathCalculator.add(num1,num2)); //aggregation concept
else if(operation==’Multiply’)
MathCalculator.printOutput(MathCalculator.multiply(num1,num2)); //aggregation concept
else
System.debug(‘Invalid Operation’);
}
}
Amit Chaudhary says:
May 19, 2021 at 10:28 amAll the best for future assignment
Krati Gupta says:
June 2, 2021 at 8:26 amComplete assignemnt 2
narmada says:
October 4, 2021 at 11:51 pmpublic class Mathcalculater {
private static integer add(integer sum1,integer sum2){
return sum1+sum2;
}
private static integer multiply(integer sum1,integer sum2){
return sum1*sum2;
}
private static void printOutput(integer sum1,integer sum2){
system.debug(‘summation value is…’+ add(sum1,sum2));
system.debug(‘product value is…’+ multiply(sum1,sum2));
}
public void doMath(integer sum1, integer sum2){
printOutput(sum1,sum2);
}
}
………………………………………Execute window
Mathcalculater MC=new Mathcalculater();
MC.doMath(5,6);
Sandeep says:
November 29, 2021 at 3:18 am/* 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) */
public class MathCalculator // Apex class mathcalculator
{
private static integer add (Integer Num1, integer num2)
{ // Private static method => Sums of 2 inputs input integer arguements and return the summation.
return Num1+num2;
}
private static integer multiply ( integer Num1 , integer num2)
{
return Num1*num2; // Private static method =Multiples 2 inputs integer adguements and multiplication.
}
private static void printout(integer Num1, integer num2)
{ // Private static method= Display addition and substraction.
system.debug(‘Addition of two values=>’+add(Num1,num2));
system.debug(‘Multiplication of two values=>’+multiply(Num1,num2));
}
public void domath(integer Num1, integer num2) // public instance method to perform domath…
{
printout(Num1,num2);
}
}
/********** Execute anonymous window*************
MathCalculator Mathcal = new MathCalculator();
Mathcal.domath(25,2);
*********** Out Put **************************
Addition of two values=> 27
Multiplication of two values=> 50
************************************************/
Manisha Sinha says:
December 24, 2021 at 1:01 pmpublic 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(‘Sum:’ + add(a,b));
System.debug(‘Multiplication:’ + multiply(a,b));
}
public void doMath(Integer a, Integer b)
{
printOutput(a, b);
}
}
Execute Anonymous window
MathCalculator mathCalculator= new MathCalculator();
mathCalculator.doMath(3,5);
Tarun soni says:
February 2, 2022 at 8:01 amAtleast give full codes in your information.
the virtual keyword code as well as the interface codes are not present.