- Apex Design Patterns
- Jitendra Zaa Anshul Verma
- 616字
- 2021-07-16 11:11:22
OOPs in play
Let's take an example of the childhood game Mario to understand OOPs. The following class shows some basic information about Mario and its capabilities:
public class Mario { public void ability(){ System.debug('I can Walk'); } public void info(){ System.debug('I am Mario'); } }
This ability to bind the capabilities of Mario in the same place is called encapsulation.
Like any other games, there are power boosters, such as speed running, bullets, and so on. If we want to change the behavior of Mario in the game, some more code can be added to the existing class with conditions. However, the chances are high that an existing application will break due to the introduction of the new code. OOP suggests that you do not modify the existing code but extend it so that testing can be done only on the new code and there are fewer maintenance issues. To resolve this, we can use Inheritance.
To use inheritance, we need to use the virtual
keyword in the base class and methods. The virtual
keyword states that a class or method can be inherited and overridden by child classes. We need to make some modification in the preceding Mario
class, informing Apex about what can be overridden. We only need to override the ability
method in the child class, so we need to mark it as the virtual
method. In order to inherit this class, it should also be declared with the virtual
keyword:
public virtual class Mario { public virtual void ability(){ System.debug('I can Walk'); } public void info(){ System.debug('I am Mario'); } }
Let's see how a child class can be written in Apex:
public class Mario_Run extends Mario {
public override void ability(){
super.ability();
System.debug('I can Run);
}
}
The following figure shows a parent-child relationship between classes:

The extends
keyword is used in a child class to inform a parent class. If we are writing the same method again in a child class of a parent class, then the override
keyword needs to be used. The override
keyword informs Apex that this is a new version of the same method in the parent class. If we want to call any method in a parent class, we need to use the super
keyword.
Run the following code as an anonymous Apex script from the developer console to understand inheritance:
Mario obj = new Mario(); obj.info(); obj.ability(); System.debug('----- Mario with power booster ----- '); obj = new Mario_Run(); obj.info(); obj.ability();
The output will look something like this:
I am Mario I can Walk ----- Mario with power booster ----- I am Mario I can Walk I can Run
As we can see, in the preceding code snippet, a child class is able to reuse a parent class method with an added behavior. The type of object is Mario
, which is the parent class, but Apex is able to call a method of the Mario_Runclass
using dynamic dispatch, which is a kind of Polymorphism.
Note
Assigning a child class reference to a parent class is known as subtype polymorphism. Read more about subtype polymorphism at https://en.wikipedia.org/wiki/Subtyping.
Static and dynamic dispatch
Types of polymorphism can be identified on the basis of when an implementation is selected. In this approach, when an implementation is selected at compile time, it is known as static dispatch. When an implementation is selected while a program is running (in case of a virtual method), it is known as dynamic dispatch.
- Objective-C Memory Management Essentials
- Learning Chef
- Mastering Yii
- Getting Started with Python Data Analysis
- MySQL數(shù)據(jù)庫基礎(chǔ)實例教程(微課版)
- Elasticsearch for Hadoop
- Raspberry Pi Home Automation with Arduino(Second Edition)
- C#程序設(shè)計(項目教學(xué)版)
- JavaScript應(yīng)用開發(fā)實踐指南
- Python 3 數(shù)據(jù)分析與機器學(xué)習(xí)實戰(zhàn)
- 工業(yè)機器人離線編程
- 零基礎(chǔ)C#學(xué)習(xí)筆記
- C語言編程魔法書:基于C11標準
- 生成藝術(shù):Processing視覺創(chuàng)意入門
- Python程序設(shè)計