As a salesforce developer, you have probably been using all OOP concepts without knowing it. The intention of this article, to provide more relevant information with an example which explains Apex as an Object-oriented programming language.
What is OOP?
- Object-Oriented Programming is about creating objects that contain both data & functions
- Class & Object are the two main aspects of OOP.
- For example, you can consider a car as an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
- A class is a template of an object & object is the instance of the class
- Apex strongly supports OOP
What are the features of Apex OOP?
The main OOPs Principles used in apex Language are,
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- In this real word Any entity that has state and behavior is known as an object. It can be physical and logical. For example, Audi Car can be considered an object.
Class:
- Class is a collection of objects. For example, The car can be considered as a class which contains different types of card like Nissan, Audi, Toyota are cars
Inheritance:
- When one object acquires all the properties and behaviors of a parent object i.e. known as inheritance. It provides code reusability.
- The concept of extending a class by another class is known as inheritance.
- A class which is inherited is known as Parent class, Base class, or Super class.
- A class which extends base class is known as Derived class or child class.
- The ‘extends’ and ‘override’ keywords used to inherit the base class, and override its method.
- As Salesforce developer we user inheritance:
- when you extend your calls from super class
- You can also extend the lightning component
- You can read more about how inheritance work from my other articles Polymorphisms & Interfaces
- When one task is performed in different ways simply it’s called polymorphism. Example-Microwave can be used for different kinds of food.
- Apex strongly supports polymorphism using method overloading & method overriding. Pls, Click here to know more about polymorphism in Apex.
- As a salesforce developer, you might be using reusable methods, from TestUtil class by passing different paraments, is the example of polymorphism.
Encapsulation:
- The action of enclosing something in or as if in a capsule, called encapsulation. Binding (or wrapping) code and data together into a single unit is known as encapsulation.
- Broadly it is defined as one of or both of the following:
- An information hiding mechanism.
- A bundling of data and methods that operate on that data.
- Example: Imagine you have a watch (or if you have one just consider it, don’t worry about the imagining part) that tells you the date and time. You can adjust the date and time using buttons/knobs/switches but you have no idea what happens inside the watch, just that it results in the date and time changing. You have some data (the date and time), you have some methods to operate on that data (buttons/knobs/switches) and there is a whole lot of stuff going on in the background that you don’t know about (and it’s better that way, imagine you had to adjust all those bits and pieces yourself). This is the beauty of encapsulation.
- As a Salesforce developer, we generally use encapsulation using:
- Classes
- Methods
- Access Modifiers
- Properties
- Variables
- Hiding internal details and showing functionality is known as abstraction. We use abstract class and interface to achieve abstraction.
- As you know now encapsulation hides the information and bind data and method, now abstraction ensure how to show relevant information.
- The benefits are that by using an abstraction that you’re making things easier for you and your team by presenting only the relevant information and tools at the appropriate time. This means a less confusing world, so less time is spent comprehending an application, fewer errors are made and everyone is happier. For example- TV has a complex internal design, however, TV manual, to operate the TV.
- Salesforce developer has probably been using Abstraction without even knowing it. Below two ways we use abstraction
- Writing a VF Page
- When an event (like updating or inserting an account) happens, businesses want to perform some operation. As a developer, you might be using Apex trigger, Handler class or batch classes to provide the solution. This is nothing but abstraction as you just providing relevant information and hiding complex process
- Abstraction and Encapsulation go hand in hand, but there is a thin line difference. Encapsulation is one of the ways you can achieve abstraction but does not encompass the entire concept. As stated previously Encapsulation is about hiding data and logic-implementations whereas Abstraction is described as providing the appropriate perspective to the appropriate consumer.
- Encapsulation ~ we are hiding the implementation.
- Abstraction ~ we are providing the appropriately simplified interface in order to access this functionality.
I really liked your blog article.Really thank you! Really Cool.
ReplyDeletedata science course in hyderabad
data science training in hyderabad