Class and Objects in Java form the foundation of how Java connects the real world with coding through object-oriented programming (OOP). By using classes and objects, developers can model real-world entities in a structured and modular way. Let’s dive into these concepts with simple examples to help you get started.
Class and Objects in Java
What is a Class?
A class serves as a blueprint or template for creating real-world entities or objects in programming. It defines the state (variables) and behavior (methods) that all objects created from it will have. A class is considered as a logical entity, meaning it doesn’t occupy memory until an object is created.
Key Points about Classes:
- Functions as a structure or blueprint.
- Includes variables (data) and methods (functions).
- Defined using the class keyword in Java.
- Allows for the creation of multiple objects from a single class.
Real-World Analogy
Imagine you’re designing a blueprint for a house:
- The blueprint outlines the features of the house – such as rooms, doors, and windows. But it doesn’t actually create the house.
- You can construct various houses (objects) from the same blueprint (class), each with unique details like different paint colors or furniture arrangements.
Basic Use of Class in Java:
In Java, a class acts as a blueprint for creating objects. Let’s explore the concept with a simple example of a Car class.
class Car {
// Data members (also instance variables)
int speed;
String color;
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
// Accessing data members
// Default value for int is 0
System.out.println("Car Speed: " + myCar.speed);
// Default value for String is null
System.out.println("Car Color: " + myCar.color);
}
}
Explanation:
Class Definition:
The Car class defines two data members:
- speed (an integer) to represent the car’s speed.
- color (a string) to represent the car’s color.
Object Creation: In the main method, an object myCar of the Car class is created using the new keyword.
Accessing Data Members: When we print myCar.speed and myCar.color, their default values are displayed:
- Default value for int is 0.
- Default value for String is null.
This basic example demonstrates how to create a class, define its properties, and access them through an object. From here, you can build more complex behaviors by adding methods and customizing properties.
What is an Object?
An Object is an instance of a class. It represents a real-world entity with specific properties and behaviors. Objects are created by using the Class.
Every object has 3 characteristics
- Identity: A unique reference to the object (e.g., its name).
- State: The values of the object’s variables (e.g., a car’s color).
- Behavior: The actions the object can perform (e.g., driving or stopping).
Real-World Analogy
Example:
class Car {
String name; // Variable
public void drive() { // Method
System.out.println("The car is driving.");
}
}
public class Passion2Code {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object
myCar.name = "Ford"; // Assigning a value
myCar.drive(); // Calling a method
}
}
Explanation:
- The Car class has one variable (name) and one method (drive).
- In the main() method, an object myCar is created.
- The name variable is set to “Ford,” and the drive() method is called to display a message.
How the Class Structure looks
A Java class typically looks like this:
Real Time Example on Classes and Objects
class BankAccount {
double balance; // Variable
public void deposit(double amount) { // Method
balance += amount;
System.out.println("Deposited: " + amount);
}
}
public class Passion2Code {
public static void main(String[] args) {
BankAccount account = new BankAccount(); // Creating an object
account.deposit(1000); // Calling a method
}
}
Explanation:
- The BankAccount class has a balance variable and a deposit method.
- An object account is created, and the deposit method is called to add money to the balance.
Summary
- A class is a blueprint that defines properties and behaviors.
- An object is an instance of a class with specific values for its properties and access to its methods.
- Together, classes and objects allow us to create real-world models in Java.
By mastering classes and objects, you can design scalable, efficient, and maintainable applications in Java.
Must Read
Java Methods
– Learn how to define and use methods in Java, including method declaration, invocation, and the importance of methods in organizing code.
Decision-Making Statements in Java
– Learn how to control the flow of your program using if, if-else, switch, and other decision-making constructs.
Looping Statements in Java
– Master the art of iteration with for, while, and do-while loops in Java.
Jumping Statements in Java
– Understand how to alter the flow of loops using break, continue, and return statements in Java.
FAQ’S
What is a class and an objects in Java?
A class is a blueprint for creating objects, defining their properties and behaviors. An object is an instance of a class with specific values and functionality.
Example:
class Car { String brand; } // Class
Car myCar = new Car(); // Object
What is a class in OOP?
In Object-Oriented Programming (OOP), a class is a logical construct that represents a real-world entity. It organizes data (variables) and behavior (methods) into a single structure.
Example:
class Animal {
void sound() { System.out.println("Animal makes a sound."); }
}
How can I create an object in Java?
You can create an object using the new keyword, which allocates memory for the object and calls the constructor.
Example:
Car myCar = new Car(); // Object of the Car class
Can a class have multiple objects?
Yes, you can create multiple objects from a single class. Each object will have its own unique state.
Example:
Car car1 = new Car();
Car car2 = new Car();
What is the difference between a class and an object?
A class is a logical entity (the blueprint), while an object is a physical entity (the real-world instance of the class).