Mastering the Basics of Object-Oriented Programming in Java
If you're new to Java programming, one of the first concepts you’ll come across is classes and objects. These two terms are fundamental building blocks of Java and the entire concept of object-oriented programming (OOP).

While these may sound technical at first, understanding them is crucial because they allow you to build real-world applications with ease, modularity, and reusability.
In this guide, we’ll break down Java classes and objects in simple terms, using easy examples and code snippets. Whether you’re preparing for interviews, learning Java from scratch, or brushing up your OOP skills, this article is for you.
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”. These objects contain both data (fields or attributes) and behavior (methods).
Java is a fully object-oriented language, meaning everything in Java is associated with classes and objects.
A class is like a blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the object created from the class will have.
Real-World Analogy:
Imagine a Car. A car has:
A Java class for a Car would define these properties and behaviors.
Basic Syntax of a Class
java
CopyEdit
Copy Code
public class Car {
// Fields (properties)
String brand;
int speed;
// Method (behavior)
void drive() {
System.out.println("The car is driving.");
}
}This class doesn't do anything on its own. To make it come to life, we need to create an object of this class.
An object is an instance of a class. It is the actual entity that exists in memory and performs actions defined in the class.
Using the Car blueprint, you can create multiple cars (objects), each with different values.
Creating an Object in Java
java
CopyEdit
Copy Code
public class Main {
public static void main(String[] args) {
// Creating an object
Car myCar = new Car();
// Assign values to object fields
myCar.brand = "Toyota";
myCar.speed = 100;
// Call method
myCar.drive();
}
}Output:
csharp
CopyEdit
The car is driving.
Here, myCar is an object of the Car class. We gave it a brand and speed and used its drive() method.
Let’s now look into a few important OOP terms in Java that relate to classes and objects.
1. Fields or Attributes
These are variables defined in the class that hold state or data about the object.
java
CopyEdit
String brand;
int speed;
2. Methods
Methods are functions inside a class that define the behavior of the objects.
java
CopyEdit
Copy Code
void drive() {
System.out.println("Driving...");
}3. Constructor
A constructor is a special method that is called automatically when an object is created. It’s used to initialize object values.
java
CopyEdit
Copy Code
public class Car {
String brand;
int speed;
// Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println("Brand: " + brand + ", Speed: " + speed);
}
}java
CopyEdit
Copy Code
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Honda", 120);
myCar.display();
}
}Output:
yaml
CopyEdit
Brand: Honda, Speed: 120
4. new Keyword
Used to create objects from a class.
java
CopyEdit
Car myCar = new Car("Tesla", 150);
5. Object Reference
The variable (myCar) does not store the object itself; it stores a reference (memory address) to the object.
Using classes and objects allows us to:
Let’s explore a few more examples to strengthen the concept.
Example 1: Student Class
java
CopyEdit
Copy Code
public class Student {
String name;
int roll;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + roll);
}
}java
CopyEdit
Copy Code
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Arpita";
s1.roll = 101;
s1.displayInfo();
}
}Example 2: BankAccount Class with Constructor
java
CopyEdit
Copy Code
public class BankAccount {
String holderName;
double balance;
// Constructor
BankAccount(String name, double initialBalance) {
holderName = name;
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
void displayBalance() {
System.out.println("Balance for " + holderName + ": " + balance);
}
}java
CopyEdit
Copy Code
public class Main {
public static void main(String[] args) {
BankAccount account1 = new BankAccount("John", 5000);
account1.deposit(1000);
account1.displayBalance();
}
}Encapsulation means keeping data private to the class and exposing it only via methods. This protects data and ensures only valid operations happen.
java
CopyEdit
Copy Code
public class Person {
private String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}Q1: Can we create objects without using new keyword?
Yes, via cloning, deserialization, or using reflection, but new is the most common and preferred method.
Q2: What happens if we don’t define a constructor?
Java provides a default constructor that initializes object fields to default values.
Q3: What is the default value of an object’s field?
Want to learn Java with real-world projects and instructor-led training?
Check out the Core Java Programming Course by Uncodemy.
Course Highlights:
Whether you’re a student or aspiring software developer, this course will strengthen your foundation in Java.
Understanding classes and objects is the first and most important step in becoming a skilled Java developer. It allows you to write clean, modular, and reusable code that mirrors real-world systems.
Let’s recap:
By practicing with multiple real-world examples, you’ll soon become fluent in writing object-oriented programs in Java.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR