In this article Class Objects in Java we give the information about Class is a blueprint or template from which objects are create and Object is an instance of class. Object follows the blueprint of the class and can use its attributes and behaviors.
Class Objects in Java
Classes, Objects and Methods in Java
Java is an Object-Oriented Programming (OOP) language, which uses classes and objects to represent real-world entities in code. Let us understand step-by-step classes, objects and methods.
-
Class:
Class is a blueprint or template from which objects are created. It defines attributes (variables) and behaviors (methods) that are common to objects. A class contains data members (variables) and methods.
class Car {
// Data Members (Attributes)
String color;
String model;
int year;
// Method (Behavior)
void start() {
System.out.println(“Car is starting…”);
}
void displayInfo() {
System.out.println(“Model: ” + model);
System.out.println(“Color: ” + color);
System.out.println(“Year: ” + year);
}
}
Explanation:
Attributes (color, model, year) and behaviors (start(), displayInfo()) are defined in the Car class.
These attributes and methods are part of the class blueprint, but they can be used only when an object is created.
-
Object:
Object is an instance of class. Object follows the blueprint of the class and can use its attributes and behaviors. When you create an object, it takes up a specific space in memory.
Example of Object:
public class Main {
public static void main(String[] args) {
// Creating an object of the class Car
Car myCar = new Car(); // Object creation
// Accessing object’s attributes
myCar.model = “Swift”;
myCar.color = “Red”;
myCar.year = 2024;
// Calling object’s methods
myCar.start();
myCar.displayInfo();
}
}
Output:
Car is starting…
Model: Swift
Color: Red
Year: 2024
-
Method:
Method is the behavior defined inside the class which represents the actions of the object. It is similar to a function, but methods are part of the class. Methods organize code and increase reusability.
return_type method_name(parameters) {
// Method body
}
Example of Methods in Class:
class Calculator {
// Method to add two numbers
int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
int subtract(int a, int b) {
return a – b;
}
// Method to display result
void displayResult(int result) {
System.out.println(“Result: ” + result);
}
}
Example of Using Methods:
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator(); // Object creation
int sum = calc.add(100, 200); // Calling add method
calc.displayResult(sum); // Output: Result: 300
int difference = calc.subtract(200, 100); // Calling subtract method
calc.displayResult(difference); // Output: Result: 100
}
}
Explanation:
There are three methods defined in the Calculator class: add(), subtract(), and displayResult().
add() method adds two integers and returns their sum.
subtract() method returns the result by subtracting two integers.
displayResult() method prints the result.
These methods are called and used by creating an object (calc).
Java Program Combining Class, Object, and Methods:
// Class Definition
class Student {
// Attributes (Data Members)
String name;
int rollNumber;
int marks;
// Method to set student details
void setDetails(String studentName, int studentRollNumber, int studentMarks) {
name = studentName;
rollNumber = studentRollNumber;
marks = studentMarks;
}
// Method to display student details
void displayDetails() {
System.out.println(“Name: ” + name);
System.out.println(“Roll Number: ” + rollNumber);
System.out.println(“Marks: ” + marks);
}
}
// Main Class
public class Main {
public static void main(String[] args) {
// Creating object of the Student class
Student student1 = new Student(); // Object creation
// Setting and displaying details of the student
student1.setDetails(“Rajveer”, 10, 95); // Set details using method
student1.displayDetails(); // Display details using method
}
}
Output:
Name: Rajveer
Roll Number: 10
Marks: 95
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining