Class & Object in Apex
Class
A class is a blueprint for the object. Before we create an object, we first need to define the class. Or we can say that It is a user-defined blueprint or prototype from which objects are created. Each class has its methods and attributes that can be accessed and manipulated through the objects.
For example, if you want to create a class for students. In that case, “Student” will be a class, and student records (like student1, student2, etc) will be objects.
Properties of Java Classes
- Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
- Class does not occupy memory.
- Class is a group of variables of different data types and a group of methods.
- A Class in Java can contain:
- Data member
- Method
- Constructor
- Nested Class
- Interface
Syntax for Creating Class in Apex
access_modifier class {
data member;
method;
constructor;
nested class;
}
Object in Apex
An object is a basic unit of Object-Oriented Programming and represents real-life entities. A class has the methods and data members (attributes), these methods and data members are accessed through an object. Thus, an object is an instance of a class. There can be multiple objects of the same class.
An object consists of :
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
If we consider the real world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior.
If we consider a dog, then its state is – name, breed, and color, and the behavior is – barking, wagging the tail, and running.
Syntax for Creating Object in Apex
Classname = new Classsname();
objectName.variable;
objectName.methods();
Example of Apex class & Object
In this example, we are creating a class “StudentInformation”. Where, the class attributes are rollNo, Name, and city. The class method is printStudentDetails().
public class StudentInformation {
public Integer rollNo;
public String email;
public String phone;
public String city;
public void printStudentDetails() {
System.debug('Student Details Are =>');
System.debug('Student Roll Number = '+rollNo);
System.debug('Student Email Address = '+email);
System.debug('Student Phone Number = '+phone);
System.debug('Student City = '+city);
}
}
Accessing Class Variable & Methods
variables and methods are accessed via created objects. To access an variable and method we need to create an object. Also we can manipulate the data using objects.
StudentInformation raj = new StudentInformation();
raj.rollNo = 101;
raj.email = 'raj@gmail.com';
raj.phone = '1234567890';
raj.city = 'Nagpur';
raj.printDetails();
Output of the Code
Student Details are =>
Student Roll Number = 101
Student Email Address = raj@gmail.com
Student Phone Number = 123456789
Student city = Nagpur
Related Topics
- List Item #1
- List Item #2
- List Item #3