Saturday 2 February 2019

Abstract Class Use in Apex

abstract keyword is used to create a abstract class and method. Abstract class cannot be instantiated. An abstract class is mostly used to provide a base for sub-classes to extend and implements the abstract methods and override or use the implemented methods in abstract class.

Abstract classes are classes which have at-least one method declared as abstract(method with keyword abstract).

When you instantiate a class extending abstract class, first abstract class constructor is called then constructor of child get called.

Lets create Abstract class and extends it





Now go to Developer console and run below code and observe the output in debug logs.

Calculator cal = new Calculator();
cal.Calculate(10,20);

below is the output:



Note: 
1. You cannot directly call the abstract class constructor i.e. if you tried to run below code you will get an error.

AbstractController abst = new AbstractController();


2. A child class must implment the abstract method of its extended class but at the same time can also use other methods and variable defined in abstract class.

No comments:

Post a Comment