OOPs in Apex

Episode 2 – Introduction to Apex (Part 2)

Apex, Apex Hours for Students, Salesforce Developer Training

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.


  • 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.



• 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;
        DEFAULT_STRING = 'Hello World';
	public Demo2(){
	public Demo2(String stringToDisplay){
		this.helloWorldString = stringToDisplay;
	public static void printOutput(){
		System.debug('Display text: ' + this.helloWorldString);

Demo2 d21 = new Demo2();

Demo2 d22 = new Demo2(‘Jigar’);

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);
        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);
        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


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


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

Further Learning

Apex Hours TrailMixes


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!

17,145 total views, 33 views today


Leave a Reply