Abstraction In Apex

Hiding the complexity and given the functionality known as Abstraction.

Real time example of Abstraction: human beings are speaking because GOD given the functionality to them for speaking but how they are speaking and what all their internal body organs they are using for speaking is completely hidden from them so finally we can say they have a functionality of speaking but complexity is completely hidden from them.

Note: Always make sure, abstraction is always for end users not for developers.

In Apex, abstraction can be achieved via 2 ways:

  • Abstract Class
  • Interface

Abstract Class: There are some points which you always remember while working on abstract class as below:

  • If you are making a class as abstract then there is no rule that you guys need to have abstract method inside the abstract class but vice-versa is not true i.e. if you guys having an abstract method inside a class in this case you guys need to declare that class as abstract.
  • An abstract class can have abstract methods (a method without body is known as abstract method) as well as concrete methods (a method with body is known as concrete method) too.
  • We can have a constructor in abstract class but we cannot instantiate the abstract class which means we cannot create object of abstract class.
  • An abstract class always works with the conjunction of child class which means it is a responsibility of child class to override the abstract method of abstract parent class.

Note: If you want that immediate child class will not override the abstract method of parent class in this case you need declare a child class a abstract class.

Syntax of an Abstract Class

public abstract class ApexAbstruct {
public ApexAbstruct() { } // default constructor
public abstract void show(); // abstract method, having no body
  public void dispaly(){	  // concrete method, having body.
    System.debug(‘This is normal method ‘); 
  }
}

apex-abstract-class

Implemented/Child Class

public class ApexClass extends ApexAbstruct{
//Override the abstract method of parent class  
public override void show(){  
    System.debug(‘This is an Astruction example’);
  }
}

apex-class-extend-abstract

Cretae an object and call the methos=d.

ApexClass ap = new ApexClass();
ap.show();

create-an-object

execuation-log1

Interfaces: There are some points which you always remember while working on interface as below:

  • All the methods of interfaces are by default abstract Which means you do not need to explicitly write abstract keyword in front of method.
  • No need to define access specifier in front of method because all the methods of interfaces are by default is global.
  • An interface always extends another interface.
  • A class always implements an interface.
  • It is a responsibility of child class to override all the methods of implemented interface as all the methods of interface are by default is abstract.

Note: Interfaces are used to achieve 100% abstraction.

Syntax of an Interface

public interface ApexInterface{
  void show();
}

apex-interface

Implemented class

public class Apex implements ApexInterface{
  public void show(){
    System.debug(‘This is Apex interface example’);
  }
}

apex-class-interface

Create an object and call the method

Apex ap = new Apex();
ap.show();

apex-call-the-method

Output:
output-execution-log

Subscribe Now