Introduction to Classes and Objects in Java
If you want to learn Java, you must understand classes and objects. These two ideas are the heart of Object-Oriented Programming (OOP).
In real life, we see objects everywhere. A car, a pen, a mobile phone—these are all objects. In Java, we also create objects to build programs.
But how do we create these objects? First, we need a class.
A class is like a blueprint. An object is a real item made from that blueprint.
Let’s take an example. A class is like a plan for a car. The actual car you drive is the object.
What is a Class in Java?
A class in Java is a way to group code. It can contain:
- Fields (also called variables)
- Methods (also called functions)
- Constructors (special methods to create objects)
A class defines what an object can have (data) and what it can do (behavior).
Syntax of a Class:
class Car {
String color;
int speed;
void drive() {
System.out.println("The car is driving.");
}
}
In this class:
- String color is a field
- int speed is another field
- drive() is a method
We have not created an object yet. This is only a template.
Characteristics 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 an Object in Java?
An object is a instance of the class. It is real and takes up memory.
You can create many objects from one 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.
How to Create an Object:
Car myCar = new Car();
Here, myCar is an object. It is created using the new keyword.
Now we can use myCar to call the method or use the variables inside the class:
myCar.color = "Red";
myCar.speed = 60;
myCar.drive();
Each object has its own data. You can make another object with different values.
If class is a recipe, the object is the dish you cook from that recipe.
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.
Class vs Object in Java
Let’s see the main differences between a class and an object.