Monday, November 10, 2014

Apex Class Definition like Virtual, abstract, extends (Inheritance)


Abstract class

public abstract class mobileabstract
{
  public string SMS{get;set;}
  public string calling{get;set;}
  public mobileabstract()
  {
  }
  public virtual decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
  }
  public abstract decimal myMethod();
 }
Extends Class

public class MultimediaPhonemobileabstract  extends mobileabstract
{
  public string mp3{get;set;}
  public string camera{get;set;}
  public integer calltype{get;set;}
  public override decimal myMethod(){
        return 52;
    }
   public override decimal Callcharge( integer t)
   {
        if(calltype==1)
        {
           return t*1.0;
        }else
        {
           return t*2.0;
        }
   }
}
  • The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined. 
  • abstract class methods MUST be overridden
Virtual class
public virtual class mobile
{
  public string SMS{get;set;}
  public string calling{get;set;}
  public mobile()
  {
  }
  public  decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
 
  }
  //public abstract decimal myMethod(); // We cannot
 use
 
 
 
 
}
Extends Class

public class MultimediaPhone extends mobile
{
  public string mp3{get;set;}
  public string camera{get;set;}
  public integer calltype{get;set;}
 
  public override decimal Callcharge( integer t)
   {
        if(calltype==1)
        {
           return t*1.0;
        }else
        {
           return t*2.0;
        }
   }

 
 /* public override decimal myMethod(){
        return 52;
    }*/
}

  • The virtual definition modifier declares that this class allows extension and overrides. You cannot override a method with the override keyword unless the class has been defined as virtual.
  • virtual class can be overridden in derived classes

 Interface

public interface mobileInterface
{

  decimal SMSCharge(integer duration);
  decimal callCharge(integer duration);
 
 
  /* We can not define
  public  decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
 
  }
 
  */
 
 
 
 
 
}
 Implements

public class NewMobile implements mobileInterface
{
   public decimal SMSCharge(integer duration)
   {
      return  duration*1.3;
   }
   public decimal callCharge(integer duration)
   {
       return  duration*1.1;
   }
   //we can add
    public decimal OtherCharge(integer duration)
   {
       return  duration*1.1;
   }
}


reference 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_defining.htm


No comments:

Post a Comment