About Me

My photo
PLANO, Texas, United States

Monday, August 18, 2014

Polymorphisms in Apex

Polymorphisms are generic terms that mean "many shaps". Allows you to use an entity in multiple forms. polymorphisms are achieved by using many different techniques named method overloading, operator overloading, and method overriding.

Overloading vs. Overriding In the apex

In Java we use the “final” keyword to stop overriding by child classes; this applies to both classes and methods, however, in apex, every class or method is final by default. So if you want to make a class/method inheritable we have to declare it either “virtual” or “abstract”. So In Java, we declare “What you can’t extend” and in Apex you declare “What you can extend”.
  1. Overloading happens at compile-time while Overriding happens at runtime.
  2. Overloading is being done in the same class while overriding base and child classes are required Overriding is all about giving a specific implementation to the inherited method of the parent class.
  3. Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
  4. Private and final methods can be overloaded but they cannot be overridden and in Apex, all methods and classes are final by default. So to override a class or a method we have to use virtual and override keywords.

Overloading

The conditions for method overloading:
  • The number of parameters is different for the methods.
  • The parameter types are different (like changing a parameter that was a float to an int).  
You cannot make overloading:
  • Just changing the return type of the method.  If the return type of the method is the only thing changed, then this will result in a compiler error.  
  • Changing just the name of the method parameters, but not changing the parameter types.  If the name of the method parameter is the only thing changed then this will also result in a compiler error. 
Example of Overloading

public class CalculateNumber
{
     public static Integer Sum(Integer FirstNum,Integer SecondNum)
     {
         return FirstNum+SecondNum;
     }
     public static Integer Sum(Integer FirstNum,Integer SecondNum,
Integer ThridNum)
     {
         return FirstNum+SecondNum+ThridNum;
     }
}


Overriding

Overriding means having two methods with the same method name and arguments (i.e., method signature). One of them is in the Parent class and the other is in the Child class.

Rules for method overriding:
  • The argument list should be exactly the same as that of the overridden method.
  • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
  • A method declared final cannot be overridden. As Apex class and method are final by default so if we want to override any method or class, we need to declare it by virtual keyword.
  • Use override keyword in the child class where you are going to override the method of the parent class.
Example of Overriding

Parent Class-

//Define Virtual keyword otherwise class will be final
public virtual class Parent_Marker
{   
    //Define virtual methods
     public virtual void write()
     {
         system.System.debug('This is in parent class..')
     }
     public virtual double discount()
     {
         return 2.5;
     }
}

Child class-

//Extend write method from Parent
public class Child_Marker extends Parent_Marker
{   
    //Define override methods
     public override void write()
     {
         system.System.debug('This is in child class..')
     }
     
}


Abstract class and methods

  • Only abstract classes can have abstract members.
  • An abstract method cannot provide any implementation means you can only declare the body. You cannot define.
  • An abstract function has to be overridden while a virtual function may be overridden.

3 comments:

  1. This is my parent's class code :-
    public virtual class DemoClass
    {
    public virtual Object sayHi()
    {
    return null;
    }
    }

    this is child class code : -
    public class DemoClass2 extends DemoClass
    {
    public override String sayHi()
    {
    return null;
    }
    }
    I am getting compile error as
    Compile Error: Method return types clash: sayHi() at line 7 column 28
    return type should be same or sub type. String is child class of Object.Can you tell what is wrong in this overriding.

    ReplyDelete
  2. it will shows like that only.... because we have to maintain same return types for both methods(virtual,override)..
    you declare in parent class method return type is object. we don't have that return type. and in chaild class you declared the return type as string... but both are wrong...
    Generally return type is based on return variable type... in parent class method you are returning null variable. So you have to give as void. and also same in chaild class method also... Then only you get the output...

    ReplyDelete