Skip to main content

Polymorphism in Apex

Polymorphism in Apex


Polymorphism in simple word can be said as One name many forms.

For example , a man can play a role as an Employee, as a Father or as a Husband in his day-to-day life. It means that he can be in any of these form. 


Polymorphism can be achieved by these two ways :

  1. Method Overloading
  2. Method Overriding

Method Overloading

    If a class have multiple methods with same name but different parameters (either different length of the arguments or different data types of the arguments), it is known as Method Overloading.
    If we have to perform only one operation, with the same name of the methods increases the readability of the program as well as its very logical too.
    Let’s suppose if we want to perform addition of the given numbers but there can be any number in arguments as below:

    Example of method overloading with different data types of the arguments with same method name like sum.

    public class MethodOverloadClass {
        public void sum(Integer no1, Integer no2) {
            System.debug('Sum of 2 Integer is : '+ (no1+no2));
        }
        
        public void sum(Decimal no1, Decimal no2) {        
            System.debug('Sum of 2 Integer is : '+(no1+no2));
        }
    }

    Execute the below code to see the output

    MethodOverloadClass objMethodOverloadClass = new MethodOverloadClass();
    objMethodOverloadClass.sum(5,4);
    objMethodOverloadClass.sum(5.4,4.2);

    Method Overriding

      If a subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Apex.
      In other words, we can say If a subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
      Note: Always make sure to achieve method overriding the scope must be of parent and child class relationship and you must have same name method needs to declare into child class with same signature of the method from Parent class to achieve method overriding in Apex.


      Why to use overriding ?

        Method overriding is used to provide specific implementation of a method that is already provided by its Parent/Super class. Method overriding is used to achieve runtime polymorphism.

        • Why to use overriding ?
          • Method name must be same as in the parent class.
          • Method name must have same parameter as in the parent class’s method.
          • Must be inheritance.


        Example ,

        here I have created a parent class and declared it as virtual , so that it can be inherited by child / other class. Also, declared the method as virtual, so that it can be overridden in child class.
        public virtual class ParentClass {
          public virtual void show() {
            System.debug('This is Parent-Class');
          }
        }
        here the Child-Class, override keyword is used to override the method.
        public class ChildClass extends ParentClass{
          public override void show(){
            System.debug('This is Child-Class');   } }
        Execute and see the the output :
        ChildClass objChildClass = new ChildClass();
        objChildClass.show();
        //OR ParentClass objParentClass = new ChildClass(); objParentClass.display();
        Output will be :
        This is Child-Class


        By : Harshal Lad 

        Comments

        Popular posts from this blog

        Salesforce Development Index

        C ourse C ontent Declarative Features Vs Programmatic Features Salesforce Architecture ( MVC ) Data Types Apex Classes If – Conditional and loops Collections in APEX List Set Map Oops Concepts Inheritance Polymorphism Encapsulation Abstraction Data Manipulation Language (SOQL) & SOSL Exception Handling Triggers and its Best Practices Custom Setting, Custom Metadata and Custom Labels Where to use it ? How to use it ? Apex Trigger + Custom Setting Asynchronous Apex Batch Classes Scheduler Classes Queueable Classes Future Method Apex Governor Limits Test Classes for Triggers and Apex classes Visualforce Pages Standard Controllers Extension Controllers Custom Controllers Wrappers Classes in Apex By : Harshal Lad ( CorpoVision Te

        Opportunities at Paidwork Company

        Unveiling the Potential of Paidwork Company : Unlocking Earning Methods, Referral Programs, and Achievements In the ever-evolving landscape of remote work and freelancing, Paidwork has emerged as a noteworthy platform that not only offers a diverse range of earning methods but also provides users with unique opportunities for growth and recognition. This article will delve into the intricacies of Paidwork, exploring its earning methods, referral program, and the benefits of achieving milestones on the platform. Go to Paidwork Website 1. Earning Methods on Paidwork: Paidwork provides freelancers and remote workers with various avenues to earn money, catering to a wide range of skills and expertise. The primary earning methods on Paidwork include: a. Freelance Projects: Users can browse through a plethora of freelance projects posted on Paidwork. From graphic design and content creation to software development and digital marketing, the platform hosts diverse opportunities for indi

        Apex Triggers and Classes

          Apex Triggers   is use to execute the functionality or piece of code on record manipulation ( i.e., CREATE / UPDATE / DELETE / UNDELETE   records ). A trigger executes before and after an event occurs on record.