Object Oriented Programming in Salesforce
Amit Chaudhary

Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates. He is a active blogger and founder of Apex Hours.

Object Oriented Programming in Salesforce

Apex is a native language that is a service-side, structured, and object-oriented language. It supports creating objects, interfaces, and classes and supports object-oriented capabilities like Abstraction, Encapsulation, Polymorphism, and Inheritance.

We will also learn about the use of the virtual, override, and super keywords.

What is Object Oriented Programming (OOPS)?

Object-Oriented Programming (OOP) is a programming technique that uses real-world platforms to create programs. This programming method encapsulates the concepts of Class, Objects, and others that fall under these categories. Following are the four pillars of Object-Oriented programming language.

  • Abstraction – The process of abstraction in Apex is used to hide certain details and only show the essential features of the object. Hiding internal details and showing only the functionality is known as abstraction. For example, when we do a phone call,  we don’t know the internal processing of it.
  • Encapsulation – Encapsulation is the ability to bind data and behavior together. For example, a capsule is wrapped with different medicines in it.
  • Polymorphism – Polymorphism is a concept which describes the process of performing a single action in different ways. In Short, Polymorphism means “many forms”. In terms of programming language,  you can declare multiple methods with the same name until they have different characteristics or parameters.
  • Inheritance – In object-oriented programming, classes can exhibit a property of acquiring some common traits and states from others classes. In other words, it is defined as the mechanism by which the child class gets to inherit the features i.e., fields and methods of the parent class. It offers code reusability and also assists in runtime polymorphism.

Before staring the OOPS concepts lets understand the class diagram which we will use to explain the concepts.

Class Diagrams

Communicate Class Structure

Class Diagrams

Class Relationships

Class Relationships

Object Oriented Fundamentals

Inheritance

Create classes that are built upon existing classes. Every Bicycle has its own features and capabilities. If we want to change the Bicycle class’s behavior and capability based on the Bicycle type, then inheritance is the best solution.

public class MountainBike extends Bicycle {    
	public void setHeight( Integer a) {        
		System.debug('setHeight');    
	}
}

We need to use the virtual or abstract keyword in the base class and methods to implement the inheritance in apex. Adding a Virtual keyword in front of the apex class or methods means they can be inherited and overridden by the child class.

Polymorphism

Use a single symbol to represent multiple different types. In apex, if you want to modify the parent class method in the child class, then we need to use the override keyword in the child class. The override keyword informs the apex that a new version of the same method is available in the child class. With the help of Super keyword, we can also access the parent class method.

NOTE: Only classes that are extending from virtual or abstract classes can use super.

Encapsulation

Restrict direct access to some of an object’s components. Let’s take an example of the Person class to understand the concept of Oops in Apex. The following code shows the basic information about Person and its capabilities:

public class Person {
    private String firstName;    
    private String lastName;
	// capabilities    
	public void getFullName() {        
		System.debug('I can eat');    
	} 
}

This ability to bind the variable and the method in the “Person” class is called encapsulation.

With encapsulation, we can protect the variable of the class. For that, we need to declare the variable as private and provide access to them with getter and setter methods.

Abstraction

The methodology of screening the internal information and elaborating things in an easy way is known as abstraction. In Apex, we can implement abstraction in two ways the abstract classes and interface. With the Abstract class, we can achieve partial abstraction, but while with interface 100% abstraction is possible

Object Oriented Programming concept Video

Share this article

2 Comments

  • I am fresher I have job opportunity Salesforce developer fresher

    • Yes, there are lots of opportunities available in market for fresher too

Leave a reply

Keep in Touch

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,230 other subscribers

Search

Our Supporter

RECENT POSTS

Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!