Object Oriented Programming in Salesforce : Amit Chaudhary

Object Oriented Programming in Salesforce
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post

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


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

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.


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.


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.


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

The post Object Oriented Programming in Salesforce appeared first on Apex Hours.

September 12, 2022 at 06:00PM
Click here for more details...

The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.