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.

Note

To use inheritance in Apex, we need to use the virtual or abstract keywords in the base class and methods.

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:

OOPs in play

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset