What is Java Class
A class in Java is a user-defined blueprint or template that describes the properties (fields/attributes) and behaviors (methods/functions) that the objects created from it will have. It essentially defines how the data will be structured and how the data will interact with methods.
Key Components of a Class:
1. Fields (Attributes): These are variables inside a class that hold the state or properties of an object. Each object created from the class will have its own copy of these fields.
Example: String name; or int age;
2. Methods: These are functions that define the behavior or actions of the class. Methods can manipulate the data (fields) of the object and can perform actions based on the object’s state.
Example: void displayFullName() { … }
3. Constructors: A constructor is a special method used to initialize objects. It has the same name as the class and is invoked when an object is created. Constructors are used to assign initial values to the fields of the class.
4. Access Modifiers: Access modifiers (private, public, protected, etc.) are used to define the visibility and accessibility of class members (fields and methods).
Example of Class
// Class definition
class Employee{
// Fields (Attributes)
String firstName;
String lastName;
int age;
// Constructor to initialize the object
Employee(String firstName, String lastName, int age) {
this.firstName = firstName; // 'this' refers to the current object's field
this.lastName = lastName; // 'this' refers to the current object's field
this.age = age;
}
// Display Full Name Method
void displayFullName() {
System.out.println("Name: " + this.firstName+ ' '+ this.lastName);
}
// Age Method
void displayAge() {
System.out.println("Age: " + this.age);
}
}
Explanation the Class
Fields: The class Employee has Three fields, firstName (a String), lastName (a String) and age (an int). These represent the state of the object.
Constructor: The constructor Employee(String firstName, String lastName, int age) is used to initialize the fields when a Employee object is created. The this keyword refers to the current object.
Methods:
displayFullName() displays the employee’s full name.
displayAge() displays the employee’s age.
What is Java Object
An object is an instance of a class. When you create an object, you are allocating memory for the class instance and initializing its fields using a constructor (if needed). The object can then access and use the methods defined in the class.
Characteristics of Objects:
1. State: The data that the object holds (values in fields/attributes).
Example: The firstName, lastName and age fields in the Employee class.
2. Behavior: The actions or methods that the object can perform.
Example: The displayFullName() and displayAge() methods that displays the object’s data.
3. Identity: Every object has a unique identity, even if it holds the same state as another object. Java uses references to identify objects in memory.
Example:
public class Main {
public static void main(String[] args) {
// Creating an object of the Employee class
Person employee1 = new Employee("John", "Taylor" 35);
Person employee2 = new Employee("Tom", "Taylor", 30);
// Calling methods on the object
employee1.displayFullName(); // Output: Name: John Taylor
employee1.displayAge(); // Output: Age: 35
employee2.displayFullName(); // Output: Name: Tom Taylor
employee2.displayAge(); // Output: Age: 30
}
}
Explanation of the Object:
Object Creation: We create two objects employee1 and employee2 of the Employee class using the new keyword. Each object is initialized with specific values for name and age through the constructor.
Method Invocation: We call the displayFullName() and displayAge() methods on the objects. These methods act on the state of the individual objects.