Apex Basic Part - II
Apex Basic Part - II
OOPS
Encapsulation
Inheritance
2. OBJECTIVES
Polymorphism
Abstraction
Oops
Object-oriented programming (OOP) is a computer
programming model that organizes software design
around data or objects rather than functions and logic.
An object can be defined as a data field that has unique
attributes and behavior.
Class -
A class is a blueprint or template that defines the structure
and behavior common to all objects created from that class.
Class contains
Note
A class is a conceptual blueprint, and it doesn't
occupy memory space at runtime.
An object is an instance of a class, and it does occupy
memory space because it represents the actual data
and functionality defined by the class.
Function in apex
A function is a block of code that performs a single
action. Functions are reusable, which helps avoid
repeating instructions. They can also improve the logical
flow of a program and make debugging easier
Custom Objects: You can define your own custom classes and
use them as return types.
Collections:
List: List<Type>
Set: Set<Type>
Map: Map<KeyType, ValueType>
Parameter
system.debug(add.Adding(5,6));
Function overloading in apex
Function overloading is a feature in object-oriented
programming languages that allows a class to have
multiple methods with the same name but different
parameter.
Readability
Overloading improves code readability by using the same
method name for similar task.
Flexibility
Function overloading provides flexibility in handling
different parameter types and numbers, accommodating
diverse scenarios
Consistent
It allows for consistent naming conventions, making the
codebase cohesive and easier to comprehend.
Constructor
A constructor is a special method in a class that initializes
the object's state when an instance of the class is
created. It is automatically invoked upon object creation .
Default constructor
This default constructor is also known as a no-argument
constructor because it doesn't take any parameters.
The primary purpose of the default constructor is to
initialize the object's state with default values or
perform any necessary setup operations.
NOTE
// Parameterized constructor
public Adder(Integer num1, Integer num2) {
number1 = num1;
number2 = num2;
calculateSum();
}
Why Constructor ?
Constructor play a crucial role in ensuring that objects start
with a well-defined state and are ready for use immediately
after creation.
Static variable and methods
A static variable is a class-level variable that is
associated with the class itself rather than with
instances of the class.
This means that all instances of the class share the
same memory for static variable.
es
Sh
ar
ar
Sh
es
Object 1 Object 2
Properties :
Accessed by Class name
Initialized only once
Retain a last value
Uses
Uses
With Encapsulation
public class BankAccount {
private Decimal balance;
public BankAccount(initialBalance)
{
this.balance = initialBalance >= 0 ? initialBalance : 0;
}
}
Public methods act as interface for
accessing private data
Without Encapsulation
public class BankAccount {
public Decimal balance;
}
Why ?
Parent class
public virtual class BankTransaction {
protected Decimal balance;
public BankTransaction(Decimal initialBalance)
{
this.balance = initialBalance >= 0 ?
initialBalance : 0;
}
Bank Transaction
Withdrawal Deposit
Polymorphism allows a
variable, function, or object
to take on multiple forms.
}
public class Dog extends Animal{
public void makeSound(String soundType)
{
if (soundType == 'Bark') {
System.debug('Dog Barks');
} else {
System.debug('Generic Dog Sound');
}
}
Why ?
Overloading allows a method to handle different types of input
data, providing adaptability to various scenarios without
sacrificing the simplicity of method names
Method overriding
public virtual class Animal {
public virtual void makeSound()
{
System.debug('Generic Animal Sound');
}
}
public class Cat extends Animal {
public override void makeSound()
{
System.debug('Cat Meows');
}
}
}
Why ?
}
public abstract Decimal calculateSalary(){
return 0.4 + 0.5 ;
}
Abstract method cannot have a body
Note -
Child class
public class PartTimeEmployee extends Employee {
IShape IColor
Rectangle
Define an interface
called Workable with
a method work().
Developer class
Manager class
public interface Workable {
void work();
}
public CompanyMember(Integer
employeeId, String name) {
this.employeeId = employeeId;
this.name = name;
}
public abstract void displayInfo();
}