What is OOPs

Object-oriented programming is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields, and code, in the form of procedures. It allows users to create the objects that they want and then create methods to handle those objects. 

The OOPs that we deal in Apex are:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation


Real World: - Any entity that has state and behavior is known as an object. It can be physical and logical. eg:- Car, bike, book, ball, etc.

Technical: An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An object contains both the data and the function, which operates on the data.

In Salesforce : 

Account objAccount = new Account(); // Object initialization

objAccount.Name = 'Test Account'; // Assigning the value to field Name of Account


Real World: It is a collection of a different objects. For example 'Car Showroom' is a class of different colors and a variety of cars (object), Properties (Data) can be price, specification, Function (Method) can be driving, reverse, braking, etc.

Technical: The class is a group of similar entities. It is only a logical component and not a physical entity.

In Salesforce :

public class salesforceExample{ 


//your methods



Real World: When an object acquires all the properties and behavior of parents. For example when a child inherits the properties of mother and father.

Technical: When one object acquires all the properties and behaviors of parent objects i.e. known as inheritance. It’s creating a parent-child relationship between two classes. It provides code reusability.

In Salesforce: For example, We use in Exception where we extend with Exception and throw own error. We can make our own class and extends in another as given below. We can also override the method by using the keyword 'Override'.

public class MyClass {

     // Some methods...


public AnotherClass extends MyClass {

// Some methods or we can override method by using the keyword Override.



Real World: When one task can be done in a different way its called Polymorphism. Let say we can make tea and coffee with some milk

Technical: Polymorphism refers to the ability of a variable, object, or function to take on multiple forms. We use method overloading and method overriding to achieve polymorphism.

In Salesforce: let's take a small example Plus '+' which can be used in Adding two numbers or we can also use in the concatenation.

Overloading occurs when two or more methods in one class have the same method name but different parameters.

public class DemoOverload{

public integer add(int x, int y){

return x+y;


public integer add(double x, int y){

return (int)x+y;



Overriding means having two methods with the same method name and parameters 

public class Car{

public static void color(){

system.debug('This is red color car');



public class TataCar extends Car{

public static void color(){

system.debug('This is Red Tata Tiago car');




Real World: Hiding the data or you can say only showing the necessary part. Let say in-car you just need Seat, Steering wheel, Gears but you don't need to look engine, petrol tank etc.

Technical: Hiding internal details and showing functionality is known as abstraction. We use abstract class and interface to achieve abstraction.

In Salesforce: Access specifier (Public, Private, Global) also acts as an Abstraction.


Real World: Binding the data together in a single unit is Encapsulation. Like the ATM machine in which we don't know inside ATM but we use that machine to withdraw money.

Technical: It is an Object-Oriented Programming concept, which binds the programming elements together in a single unit that manipulate the data, and that keeps both safe from outside interference and misuse

In Salesforce: Achieved by CLASS & INTERFACE. (like Database.Batchable, Database.BatchableContext, Database.StateFull)

global class BatchClass implements Database.Batchable<sObject>{