Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects, rather than functions or logic. Java is a purely object-oriented language (except for primitive types) and heavily relies on OOP concepts such as classes, objects, constructors, encapsulation, and access control. Understanding these concepts is essential for building real-world, scalable, and maintainable Java applications.
A class is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods) that an object will have.
class ClassName {
// variables
// methods
}
An object is an instance of a class. It represents a real-world entity and occupies memory.
new keywordclass Student {
String name;
int age;
void display() {
System.out.println(name + " " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
s1.display();
}
}
Student is a class, and s1 is an object of that class.
A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type.
A constructor with no parameters is called a default constructor.
class Demo {
Demo() {
System.out.println("Default constructor called");
}
}
A constructor that accepts parameters is called a parameterized constructor.
class Employee {
int id;
String name;
Employee(int i, String n) {
id = i;
name = n;
}
}
A copy constructor creates a new object by copying values from another object.
class Book {
String title;
Book(Book b) {
title = b.title;
}
}
The this keyword refers to the current object of the class. It is mainly used to differentiate between instance variables and parameters with the same name.
thisclass Person {
String name;
Person(String name) {
this.name = name;
}
}
this.name refers to the instance variable, while name refers to the constructor parameter.
Access modifiers control the visibility and accessibility of classes, variables, methods, and constructors.
| Modifier | Accessibility |
|---|---|
public | Accessible everywhere |
private | Accessible only within the same class |
protected | Accessible within same package or subclass |
default | Accessible within same package |
class Test {
public int a = 10;
private int b = 20;
}
Encapsulation is the process of wrapping data and methods together into a single unit (class).
Data hiding is achieved by declaring variables as private and restricting direct access from outside the class.
class Account {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
Getters and setters are public methods used to access and modify private variables safely.
class Student {
private int marks;
public void setMarks(int m) {
if (m >= 0 && m <= 100) {
marks = m;
}
}
public int getMarks() {
return marks;
}
}
Direct access to marks is restricted. Validation is applied using setter.
java
public class Student {
// Private fields (encapsulation)
private String name;
private int rollNumber;
private double marks;
// Default constructor
public Student() {
this.name = "Unknown";
this.rollNumber = 0;
this.marks = 0.0;
}
// Parameterized constructor
public Student(String name, int rollNumber, double marks) {
this.name = name;
this.rollNumber = rollNumber;
this.marks = marks;
}
// Copy constructor
public Student(Student other) {
this.name = other.name;
this.rollNumber = other.rollNumber;
this.marks = other.marks;
}
// Getters
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
// Setters
public void setName(String name) {
this.name = name;
}
public void setMarks(double marks) {
if (marks >= 0 && marks <= 100) {
this.marks = marks;
}
}
// Method
public void display() {
System.out.println("Name: " + name);
System.out.println("Roll Number: " + rollNumber);
System.out.println("Marks: " + marks);
}
public static void main(String[] args) {
Student s1 = new Student("John", 101, 85.5);
s1.display();
Student s2 = new Student(s1);
s2.setName("Jane");
s2.display();
}
}