Christmas Special : Upto 40% OFF! + 2 free courses  - SCHEDULE CALL

sddsfsf

Oops Concept in Salesforce

 

Object refers to any real-world entity such as tree, car, table, lamp etc. and Object-Oriented Programming (OOPs) is a way or methodology used for designing a program that includes classes and objects. The process of software development and maintenance becomes easier with the help of OOPs in salesforce.

An Overview of Apex OoPs Concepts

Apex Oops Concepts are divided into different categories described as follows:

Object: A real-world entity that possesses behavior and state is called as Object in salesforce. It is the implementation or instance of any class. For example, monitor, marker, pen, book etc. Understanding the apex oops concepts in apex salesforce with examples of an object in detail can be like: Marker is object, Luxor is its name, its color is blue; this is the state data of the object marker, and it is used in writing, therefore writing is referred as its behavior method. 

What is a New Keyword in Apex?

New keyword in Apex is used for allocating the memory at runtime. Every object gets memory in the heap memory area.

Class: A class is logical entity given to different types of objects. For example, all types of trees can come under the class Tree. Thus, an individual object of a class is created. Technically, class can be defined as a prototype or concept or template, which has member data and member method. It can be referred to group of objects that possess common properties. 

Encapsulation: Single unit formed by binding of data and code together is known as Encapsulation. Like, a capsule is formed by wrapping up different medicines together. Any Apex Class can be referred as Encapsulation. All data members are private in an Apex bean and thus it is a fully encapsulated class. 

Polymorphism: One name that has several forms is called as polymorphism. For example, a person can have different traits, like he/she can be artist, homemaker and cook at the same time, or a boy can be a brother, son and friend at the same time. To achieve polymorphism in Apex, method overriding and method overloading are used. TO understand in more detail subscribe to online Salesforce Training.

Method Overriding – When a child class or subclass possesses the same method as the parent class, it is termed as method overriding. A subclass providing specific method implementation as provided by any of its parent classes is called as method overriding. It should be ensured that method overriding is achievable only when there is a parent child relationship and same method and signature should be declared into the child class. Method overriding is used for achieving runtime polymorphism. 

Rules for Method Overriding in Apex:

  • Method name should be the same as in its parent class
  • Method name should have the same parameter as in its parent class’s method
  • It should be IS-A relationship (inheritance)

Method Overloading : When multiple methods have the same name in a class but possess distinct parameters, then it can be termed as method overloading. So, in case a person needs to perform just one operation, the same name of the methods can increase the readability of program. Also, it is very logical.

Real Life Example: Suppose a director in a college wants to call all head of departments for an annual meeting, so he publishes a notice for the same, the notice will be pointing out all the names of HODs. Or, he can frame an email and send to all HODs at the same time. Such a concept is termed as polymorphism. 

Inheritance: If a class acquires every property or traits of the parent or super-parent class, then it is called as inheritance. This also provides code reusability for achieving the runtime polymorphism. (Inheritance is mandatory for achieving polymorphism)

When one inherits from any existing class, then fields and methods from parent class can be sensed, and new fields and methods can also be added to the same. Inheritance represents child-parent relationship. Inheritance is used for code reusability and method overriding.

Syntax for Apex Inheritance

public virtual class Parent{
public void display(){  
System.debug(‘This is Parent’);    
}  
}

Syntax of Sub Class

public class Child extends Parent{
public void display show(){  
System.debug(‘This is form Child’);    
}  
}

Creating Object and Call Method

Child c = new Child();
c.show();
c.display();

Abstraction: It refers to showing functionality and hiding of internal complexity. For example, running a mixer, in which we are not aware and bothered about the internal processing. To achieve abstraction in Apex, we make use of abstract class and interface. 

In all, screening of internal information and elaborating of thing in easy manner is called as Abstraction in Apex. It can be implemented in two ways, namely, interface and abstract classes. With the help of Abstract class, partial abstraction can be achieved while 100% of abstraction is achievable through interface. 

If we talk about abstraction in real world, then there are three levels. Those are physical level, view level and conceptual level. Physical level abstraction is about the architecture of application. Designing of any problem into application form is a physical level abstraction. View level abstraction is about retrieving of data from database in different combinations. It is available for all internet users. Conceptual level abstraction is not about the physical architecture but about the technical and foundation mechanism. Applying testing principle and coding is a type of conceptual level abstraction.

Apex Method in Salesforce

Salesforce is a best cloud computing software, facilitating businesses to mange customer data. Explore cloud computing courses to master the cloud  domain. 

The mechanism or way to perform any operation is known as method in salesforce OOPS concepts. It is a collection of the instructions required for performing of the task and provides reusability of the code. Set of codes or statements compiled together can be capable of performing any required operation in salesforce. By modifying the involved codes, those could be reused and therefore a method is written just once but it is used for several times for performing several operations. 

Declaration of method gives the detailed information about it, like return-type, visibility, name and the segments as shown in the following figure:

            

Every method has a unique method signature, which is the part of method declaration. The signature contains parameter/argument list and method name. With the help of our salesforce developer interview questions, you can help you crack the job interview and also get an idea of how much knowledge you have acquired in OOPs in Apex and  salesforce.https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_learning_apex.htm

Access Specifier: It is the access type method that specifies the method visibility from where our method can be called. Java provides four different types of access specifiers:

  • Global- It is accessible by all salesforce classes whether within or outside salesforce
  • Public- It is accessible by all classes that are within salesforce object-oriented programming
  • Private- When using a private access specifier, the method is accessible only within the class in which it was defined
  • Protected- while using a protected access specifier in Apex, the method can be accessible to any of the inner classes of defining Apex class, and to classes that could extend defining Apex class as per the requirement. 

Return Type: It is the data type that the method would be returning. It could have object, primitive data type, void, collection etc. as per the need. If the method returns nothing, then void keyword is used. Void means that the method would not return any kind of value.  

Method Name: It is the unique name given to a method. It should be assured that the name selected for your method corresponds to the method functionality. By reading the method name, it could be understood about whatever the method would be performing. 

Method Body: Business logic is defined in the method body, and is enclosed within the curly braces pair, which defines the method scope.

Parameter/Argument List: This list contains the arguments/parameters separated by comma, which has variable name and data type. If the method contains no parameter, then the parentheses are left as blank.

Constructor in Apex

Initializing the object is possible with the help of a special type of method known as Constructor. At the time of creating an object, Apex constructor gets invoked and it gives data for object. The name of a Constructor is always same as the name of its class and it must not have any explicit return type. Two types of constructors are Default (constructor without parameter) and Parameterized. 

Syntax used for a constructor is same as that for a method. No explicit return type exists and these cannot be inherited by an object.

Once a constructor written for any class, then you should necessarily use new keyword. It will call the object from the class by using constructor. Example is given as follows:

public class TempObject {
   // The no argument constructor 
   public TempObject() {
      // more code here
  }
}
New object can be instantiated by the below code:
TempObject myTemp = new TempObject();

When you construct a constructor that would take parameters, then the object could be created by using those arguments with that constructor If you have created a constructor that would take arguments but want to use a no-parameter constructor, then you should create custom constructor that is no-argument in nature. Once the constructor is created, then you cannot access the default no-argument public constructor. 

Any constructor could be overloaded, means a class could contain more than one constructor, with different parameters for each. Following example portrays a class that has two constructors, one is no-argument and other that takes simple integer argument. This also defines  how one constructor would called another using (...) syntax, called as constructor chaining.

as constructor chaining.
public class TempObject2 {
private static final Integer DEFAULT_SIZE = 10;
Integer size;
   //Constructor with no parameters
   public TempObject2() {
       this(DEFAULT_SIZE); // Using this(...) calls the one argument constructor    
   }
   // Constructor with one argument 
   public TempObject2(Integer ObjectSize) {
     size = ObjectSize;  
   }
}
New objects of this type can be instantiated with the below code:
TempObject2 myObject1 = new TempObject2(42);
  TempObject2 myObject2 = new TempObject2();

Each constructor created for a class must have different argument list. In the example as follows, all constructors are possible:

public class temp {
  //  no-argument constructor 
  public temp () {}
  // Constructor with one argument
  public temp (Boolean put) {}
  // Constructor with two arguments
  public temp (String name, Boolean put) {}
  // Though this constructor has the same arguments as the 
  // one above, they are in a different order, so this is legal
  public temp (Boolean put, String name) {}
}

While defining a new class, one defines new data type. Class name can be used in any place and data type names can be used, like Boolean String, Account etc.

Apex Constructor Overloading

Constructor in Apex is like the method without any return type. And, it can be overloaded like the methods in Apex. The technique of Apex Constructor overloading is about acquiring more than a constructor with lists of different parameters but same name. Every constructor performs a different task according to their basic arrangement style. A compiler differentiates the arguments/parameters and their types in the list. 

Apex Classes Association

Association defines connection or relation between two different classes in Object-Oriented Programming (OOPs). In Apex object oriented programming concepts, programming hints about how the objects are related to each other and what functionality is needed. Association in Apex is achieved in two ways portrayed as under:

To become a salesforce developer, creating an understanding of Object-Oriented Programming is important. Establishing association and inheritance is possible only with the help of training about the technical terms and coding. Are you curious to know about the future growth of Salesforce developers? Click here.

Salesforce Training For Administrators & Developers

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available
cta12 icon

Conclusion

After going through with all that can has been said in the above blog, we can say that concepts of object oriented programming are well embedded in salesforce apex. These concepts make the life of developer easy and makes possibility of reuse of the code.

Trending Courses

Cyber Security icon

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security icon1

Upcoming Class

0 day 27 Dec 2024

QA icon

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA icon1

Upcoming Class

1 day 28 Dec 2024

Salesforce icon

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce icon1

Upcoming Class

3 days 30 Dec 2024

Business Analyst icon

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst icon1

Upcoming Class

0 day 27 Dec 2024

MS SQL Server icon

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server icon1

Upcoming Class

0 day 27 Dec 2024

Data Science icon

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science icon1

Upcoming Class

7 days 03 Jan 2025

DevOps icon

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps icon1

Upcoming Class

-1 day 26 Dec 2024

Hadoop icon

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop icon1

Upcoming Class

1 day 28 Dec 2024

Python icon

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python icon1

Upcoming Class

0 day 27 Dec 2024

Artificial Intelligence icon

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence icon1

Upcoming Class

8 days 04 Jan 2025

Machine Learning icon

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning icon1

Upcoming Class

0 day 27 Dec 2024

 Tableau icon

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau icon1

Upcoming Class

1 day 28 Dec 2024