Access Modifiers And OOPS Concepts

26 October, 2018


Related Blogs

Access Modifiers And OOPS Concepts

Access Modifiers:

Apex allows us to use the following modifiers


This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If we do not specify an access modifier, the method or variable is private.


This means that the method or variable is visible to any inner classes in the defining Apex class. We can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.


This means the method or variable can be used by any Apex in this application or namespace.

Note: In Apex, the public access modifier is not the same as it is in Java. This was done to discourage joining applications, to keep the code for each application separate. In Apex, if we want to make something public like it is in Java, we need to use the global access modifier.


This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If we declare a method or variable as global, we must also declare the class that contains it as global.

Note:We recommend using the global access modifier rarely, if at all. Cross-application dependencies are difficult to maintain.

Keyword use are

With Sharing: Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user.

Without Sharing: Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are notenforced.


An interface is like a class in which none of the methods have been implemented—the method signatures are there, but the body of each method is empty. To use an interface, another class must implement it by providing a body for all of the methods contained in the interface.


The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.


Inheritance concept


Final variables can only be assigned a value once, either when we declare a variable or in initialization code


The virtual definition modifier declares that this class allows extension and overrides.


There are two different ways of using the this keyword. We can use the this keyword in dot notation, without parenthesis, to represent the current instance of the class in which it appears. Use this form of the this keyword to access instance variables and methods. For example:

Public class myTestThis {
 string s;
 this.s = 'TestString';

In the above example, the class myTestThis declares an instance variable s. The initialization code populates the variable using the this keyword. Or we can use the this keyword to do constructor chaining, that is, in one constructor, call another constructor. In this format, use the this keyword with parentheses.

For example:

public class testThis {
 // First constructor for the class. It requires a string parameter.
 public testThis(string s2) {
 // Second constructor for the class. It does not require a parameter.
 // This constructor calls the first constructor using the this keyword.
 public testThis() {


The super keyword.can be used by classes that are extended from virtual or abstract classes. By using super, we can override constructors and methods from the parent class. The following are special types of keywords that aren't reserved words and can be used as identifiers.

Count: count() returns the number of rows that match the filtering conditions and count() returns the number of rows that match the filtering conditions and have a non-null value for . This syntax is newer than COUNT() and is available in API version 18.0 and later.

Enthusiastic about exploring the skill set of Salesforce?Then, have a look at the Salesforce Training Online to gather additional knowledge.

OOPS Concepts

Apex supports the following OOPS concepts given below


Acquiring the properties of parents by children is what we call as. We can make use of the same principle under object oriented language and consume the members of one class under multiple classes by establishing Parent-Child relationship between classes. This basically provides us  


Changing the behavior of entities depending upon input they receive is known as Polymorphism. This can be implemented in object oriented language using different approaches like

  • Overloading: It is a feature that allows us to define multiple behaviours to a method
  • Overriding: To override parent classes method under child class first in the parent class the method must be declared as 'Virtual'. Then only child classes can override the method using   modifier.


This is all about Hiding the complexity of a program and providing with a set of interface to consume the functionalities. Functions and Methods are examples for Abstraction we never know the logic behind them, what we are is only how to consume them.


Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of

Data Hiding

It is all about hiding the code to secure the contents of a program. To hide the code we enclose the members of a program under a container or wrapper known as class.


A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visual force this type of class can be extremely useful


A function or Method in general referred as Subprogram is a named block of code which can be reused, when and where is called its get executed It is also an action that has to be performed A method can be either value returning or non value returning, use * - to tell the method non value returning and if it value returning specify the type (primitive or Sobject data type) of value it was returning using   keyword.

Some Other key words are


For indepth understanding on Salesforce click on

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .