Thursday, April 13, 2023

Polymorphism in Apex Programming with Example

Polymorphism is a concept in object-oriented programming where a single method or function can be used to perform different operations based on the type of object that it is called on. In Apex, polymorphism can be achieved using interfaces, abstract classes, and virtual methods. Here's an example of polymorphism in Apex using interfaces:

// Define an interface
public interface Shape {
    void draw();
}


// Define classes that implement the Shape interface
public class Circle implements Shape {
    public void draw() {
        System.debug('Drawing a circle');
    }
}


public class Rectangle implements Shape {
    public void draw() {
        System.debug('Drawing a rectangle');
    }
}


// Call the draw method on different objects of type Shape
Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.draw(); // Output: Drawing a circle
s2.draw(); // Output: Drawing a rectangle

In the above example, we define an interface called Shape with a method called draw(). We then define two classes, Circle and Rectangle, that implement the Shape interface and provide their own implementation of the draw() method.

Finally, we create two objects of type Shape, one of type Circle and one of type Rectangle. When we call the draw() method on each of these objects, the appropriate implementation of the draw() method is called based on the type of the object, demonstrating polymorphism.


Let me know your thoughts in the comment section!!

Interfaces in Apex Programming with Example

In programming, an interface is a collection of abstract methods and constants that can be implemented by a class. Here are some uses of interfaces in programming:

Abstraction: Interfaces allow you to define a contract or a set of rules that must be followed by any class that implements the interface. This helps to separate the implementation details from the interface, making the code more modular and easier to maintain.

Polymorphism: Interfaces allow you to achieve polymorphism in your code. Since any class that implements the interface must provide an implementation for all of the methods in the interface, you can create objects of different classes that implement the same interface and treat them as if they were the same type.

Multiple Inheritance: In some programming languages, a class can only inherit from one parent class. However, a class can implement multiple interfaces, allowing it to inherit from multiple sources. This makes it easier to create complex class hierarchies without having to rely on multiple inheritance.

Lets understand the concept with the help of an example.

public interface Interest{
    Decimal calculatePaymentAmount();
}

In this example, we define an interface called Interest that has one method called calculatePaymentAmount(). Any class that implements the Interest interface must provide an implementation for the calculatePaymentAmount() method.

Here's an example of a class that implements the Interest interface:

public class HomeLoanPayment implements Interest {
    public Decimal calculatePaymentAmount() {
        // implement your logic
    }
}

In this example, the HomeLoanPayment class implements the Interest interface by providing an implementation for the calculatePaymentAmount() method. This method calculates the payment amount for a Home Loan.

By using an interface in this way, we can define a common set of methods that must be implemented by any class that implements the interface. This allows us to write more flexible and reusable code.


Where do we use Interfaces in Apex

Batch Apex: Developer needs to implement the Database.Batchable interface. Thats why we need to write three methods namely, Start, Execute and Finish.

Queueable Apex: In this scenario, developer needs to implement Queueable interface and implement the Execute method.


Let me know your thoughts in the comment section!!

Saturday, April 8, 2023

Static Variables and Static Methods in Apex

Static variables are variables that belong to an overall class, not a particular object of a class. Think of a static variable to be like a global variable – its value is shared across your entire org. Any particular object’s properties or values are completely irrelevant when using static. When a class containing static variables is first referenced, the static variables are initialized. The initialization process occurs only once per execution context, regardless of how many times the class is referenced.

This example illustrates static vs. non-static variables

public class classexmple {
  public Integer count = 0;
  public static Integer Scount = 0;
  public void executeMethod() {
    count = count + 1;
    Scount = Scount + 1;
  }
classexmple obj1 = new classexmple();
classexmple obj2 = new classexmple();
obj1.executeMethod();
obj2.executeMethod();
System.debug(obj1.count); // This is 1
System.debug(obj2.count); // This is 1
// Access static variables only by class name, 
not object name
System.debug(classexmple.Scount); // This is 2

Static methods, similarly, are methods that act globally and not in the context of a particular object of a class. Being global, static methods only have access to its provided inputs and other static (global) variables.

This example illustrates static vs. non-static methods

public class SecondExample {
  public void Method1() {
    // code
  }
  public static String Method2() {
    // code
    String a;
    return a;
  }
  public static String Method3(String param) {
    String a;
    //code
    return a;
  }
}

SecondExample obj = new SecondExample();
// Non-static methods require an object 
for context
obj.method1();
// Static methods aren't related to a 
particular object
// You don't even use an object to call a 
static method!
SecondExample.Method2();
SecondExample.Method3('value');


Let me know your thoughts in the comment section!!

Monday, April 3, 2023

What are Remote Site Settings in Salesforce

Salesforce allows you to access external website resources from a Salesforce application for your organization. You can access these external websites through Visualforce pages, Apex Callout, and via XmlHttpRequest calls. To avoid accessing malicious websites from Salesforce.com, the website first needs to be registered with remote site settings. Once the site is registered, it can be used within Salesforce.com. 

To Setup the Remote Site Setting 

  • search and click remote site settings in setup.site
  • Click on New Remote Site and enter the URL and other fields required.
  • Click Save.

Please find the description of the fields in remote site settings

site

Let me know your thoughts in the comment section!!

Friday, March 31, 2023

Wrapper Class in Salesforce

Wrapper Class in Apex Salesforce

In simple words, a wrapper class is a class inside a class. It is also known as a container class. It stores a group of similar or different data type variables into a single object.

Lets understand with an example:


public class ExampleCls {
    public class WrapperCls{
        public Boolean selected { getset;}
        public String record  { getset;}
        
    }
}

In this example, ExampleCls is the main class and
WrapperCls is the wrapper or the inner class.

This wrapper class contains two different data types,
one Boolean and other one is String

Use of Wrapper Class:

  1. It helps to stores a group of similar or different data type variables into a single object.
  2. We can use a wrapper class in Lightning Component when we need to send group of data from apex to lightning components.
  3. We can use Wrapper classes to parse the JSON request and response in REST API.

Mostly Viewed