OOPs concepts in Apex
OOP (Objecte Oriented Programming) is a methodology, that provides a way of modularizing a program by creating partitioned memory area for both data and methods that can be used as template for creating a copies of such objects(Models) on demand.
What is the need for OOPs?
There are many reasons why OOPs are mostly preferred, but the most important among them are: OOPs help users to understand the software easily, although they don’t know the actual implementation. With OOPs, the readability, understandability, and maintainability of the code increases multifold. Even very big software can be easily written and managed easily using OOPs.What are the limitations of OOPs?
- Requires intensive testing processes.
- Solving problems takes more time as compared to Procedure Oriented Programming.
- The size of the programs created using this approach may become larger than the programs written using the procedure-oriented programming approach.
- Software developed using this approach requires a substantial amount of pre-work and planning.
- OOP code is difficult to understand if you do not have the corresponding class documentation.
- In certain scenarios, these programs can consume a large amount of memory.
- Not suitable for small problems.
- Takes more time to solve problems.
The main OOPs Principles used in apex Language are,
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
1. Inheritance:
It creates new classes from existing classes so that the new classes will acquire all the features of the existing classes is called Inheritance.When one object acquires all the properties and behaviors of 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.
Example, Inheritance in nature is parents producing the children and children inheriting the qualities of parents.
2. Encapsulation:
The wrapping up of data and methods together is called encapsulation. orBinding (or wrapping) code and data together into a single unit is known as encapsulation.
Example, a capsule, it is wrapped with different types medicines into single unit.
Example, if we take a class we write the variables and methods inside the class. Thus, a class is binding them together. So a class is an example of encapsulation. It is also known as Data Hiding.
3. Abstraction:
Abstraction is the concept of object-oriented programming that "shows" only essential attributes and "hides" unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users.Hiding internal complexity and showing functionality is known as abstraction.
For example: Car Drive, we don’t know the internal processing.
In Apex, we use abstract class and interface to achieve abstraction.
There are two ways to achieve abstraction:
- Abstract class (0 to 100%)
- Interface (100%)
What is the interface?
An interface is a blueprint of a class. It has static constants and abstract methods. The interface is a mechanism to achieve abstraction. There can be only abstract methods in the interface, not method body. It is used to achieve abstraction and multiple inheritance. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
# Rule for interface:
- Interface keyword must be used to declare interface
- Interface must have signature but not a method body
- To implement interface in class we have to use implement keyword
- We must have to provide the body to method of interface in the implemented class
- It archives 100% abstraction.
- We are not able to provide access specifier to the method signature in the interface.
- We cannot create an object for the interface.
- We have to use extends keyword in between 2 interfaces
- We have to use the implements keyword in between class and interface.
# Rule for abstract class:
- Abstract keyword must be used to declare abstract Class
- Abstract Class can have abstract and non-abstract(Regular Method) methods.
- To use abstract class in child class we have to use extends keyword
- We must have to provide the body to method of abstract method in the implemented class (child class)
- It archives 0-100% abstraction.
- We cannot create an object for the Abstract Class
- We cannot use static keyword for abstract method signatures in abstract class.
- Regular/normal method of abstract class can be override by the child class method.
Difference between Abstract class and Interfaces :
Abstract class | Interface |
---|---|
Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. |
Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
An abstract class can extend another class and implement multiple apex interfaces. | An interface can extend another interface only. |
Total number of SOQL queries issued Synchronous Limit : 100 Asynchronous Limit : 200 |
Total number of SOSL queries issued : 20 |
An abstract class can be extended using keyword "extends". | An interface can be implemented using keyword "implements". |
Example: public abstract class abstractClass{ abstract void draw(); } |
Example: public interface interfaceItem{ void draw(); } |
4. Polymorphism:
Polymorphism represents one form in multiple forms. In programming, we can use a single variable to refer to objects of different objects which means that single variable you can refer in the second object can be an extension of the first object. Thus using that variable we call the methods of different objects. A method call can perform different tasks depending on the type of the object.Example, Microwave can be used for different kinds of food.
Apex strongly support polymorphism using method overloading and method overriding.
- Overloading occurs when two or more methods in one class have the same method name but different parameters.
- Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class.
Comments
Post a Comment