Object-Oriented Programming (OOP) is one of the core concepts of Java. Unlike procedural programming, where programs are divided into functions, OOP organizes programs into objects and classes. This approach makes software easier to develop, maintain, reuse, and scale.
Java is a pure object-oriented programming language (except for primitive data types), and almost every real-world Java application is built using OOP principles.
OOP is widely used in:
- Banking Systems
- E-commerce Applications
- Hospital Management Systems
- Student Management Systems
- Android Applications
- Enterprise Software
- Desktop Applications
- Game Development
- Inventory Management Systems
The four pillars of Object-Oriented Programming are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Before learning these advanced concepts, you must first understand:
- Classes
- Objects
- Instance Variables
- Instance Methods
- Constructors
In this chapter, you’ll practice beginner-friendly Java OOP programs that build a strong foundation for advanced Java development. Java Object-Oriented Programming (OOP) practice questions with solutions help to understand the concepts.
1. Java Program to Create a Class and Object
Problem Statement
Write a Java program to create a class named Student, create an object of the class, and display a message.
Java Solution
class Student {
void display() {
System.out.println("Welcome to Java OOP");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.display();
}
}
Sample Output
Welcome to Java OOP
Explanation
- A class named
Studentis created. - An object named
studentis created using thenewkeyword. - The object’s method is called using the dot (
.) operator.
Concepts Covered
- Class
- Object
- Method Calling
- new Keyword
2. Java Program to Store and Display Student Information
Problem Statement
Write a Java program to create a class that stores a student’s name and age, then displays the information.
Java Solution
class Student {
String name = "Rahul";
int age = 21;
void display() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.display();
}
}
Sample Output
Name : Rahul
Age : 21
Explanation
The class contains two instance variables:
nameage
The display() method prints both values.
Concepts Covered
- Class
- Object
- Instance Variables
- Instance Method
3. Java Program to Calculate the Area of a Rectangle Using a Class
Problem Statement
Write a Java program to create a class that calculates the area of a rectangle.
Java Solution
class Rectangle {
int length = 10;
int width = 5;
void area() {
int result = length * width;
System.out.println("Area = " + result);
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.area();
}
}
Sample Output
Area = 50
Explanation
The class stores:
- Length
- Width
The area() method calculates:
Area = Length × Width
Concepts Covered
- Class
- Object
- Instance Variables
- Instance Method
- Arithmetic Operations
4. Java Program to Use a Default Constructor
Problem Statement
Write a Java program to create a class with a default constructor that displays a welcome message.
Java Solution
class Student {
Student() {
System.out.println("Default Constructor Called");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
}
}
Sample Output
Default Constructor Called
Explanation
A constructor is a special method that is automatically executed whenever an object is created.
In this program:
- The constructor name is the same as the class name (
Student). - It has no parameters, so it is called a default constructor.
- The constructor is executed automatically when the object is created.
Concepts Covered
- Constructor
- Default Constructor
- Class
- Object Creation
5. Java Program to Use a Parameterized Constructor
Problem Statement
Write a Java program to create a class that uses a parameterized constructor to initialize student details.
Java Solution
class Student {
String name;
int age;
Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
void display() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Rahul", 22);
student.display();
}
}
Sample Output
Name : Rahul
Age : 22
Explanation
Unlike a default constructor, a parameterized constructor accepts values while creating an object.
Student student = new Student("Rahul", 22);
The constructor initializes the instance variables using the provided values.
Concepts Covered
- Parameterized Constructor
- Constructor Parameters
- Instance Variables
- Object Initialization
6. Java Program to Create Multiple Objects of a Class
Problem Statement
Write a Java program to create multiple objects of a class and display their details.
Java Solution
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student("Rahul", 21);
Student student2 = new Student("Priya", 22);
student1.display();
student2.display();
}
}
Sample Output
Name : Rahul
Age : 21
Name : Priya
Age : 22
Explanation
A class can have multiple objects.
Each object maintains its own copy of the instance variables.
Here:
student1stores Rahul’s information.student2stores Priya’s information.
Concepts Covered
- Multiple Objects
- Constructors
- Instance Variables
- Object-Oriented Programming
7. Java Program to Display Employee Details Using Objects
Problem Statement
Write a Java program to create an Employee class that stores employee ID, name, and salary, then displays the employee details.
Java Solution
class Employee {
int id;
String name;
double salary;
Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
void display() {
System.out.println("Employee ID : " + id);
System.out.println("Name : " + name);
System.out.println("Salary : " + salary);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee(101, "Amit", 55000);
employee.display();
}
}
Sample Output
Employee ID : 101
Name : Amit
Salary : 55000.0
Explanation
The Employee class stores employee-related information.
The constructor initializes the data, while the display() method prints it.
Concepts Covered
- Classes
- Objects
- Constructors
- Employee Management Example
8. Java Program to Calculate Circle Area Using a Class
Problem Statement
Write a Java program to calculate the area of a circle using a class and object.
Java Solution
class Circle {
double radius;
Circle(double radius) {
this.radius = radius;
}
void calculateArea() {
double area = 3.14159 * radius * radius;
System.out.println("Area = " + area);
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(7);
circle.calculateArea();
}
}
Sample Output
Area = 153.93791
Explanation
The program stores the radius using the constructor.
The calculateArea() method computes:
Area = π × r × r
Concepts Covered
- Classes
- Objects
- Constructors
- Mathematical Calculations
9. Java Program to Demonstrate the this Keyword
Problem Statement
Write a Java program to demonstrate the use of the this keyword.
Java Solution
class Student {
String name;
Student(String name) {
this.name = name;
}
void display() {
System.out.println("Student Name : " + this.name);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Rohan");
student.display();
}
}
Sample Output
Student Name : Rohan
Explanation
The this keyword refers to the current object.
It is commonly used to differentiate between:
- Instance variables
- Constructor parameters
Example:
this.name = name;
Here:
this.name→ Instance variablename→ Constructor parameter
Concepts Covered
- this Keyword
- Constructors
- Instance Variables
10. Java Program to Create a Bank Account Class
Problem Statement
Write a Java program to create a BankAccount class that stores account holder information and balance.
Java Solution
class BankAccount {
String accountHolder;
double balance;
BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
void displayAccount() {
System.out.println("Account Holder : " + accountHolder);
System.out.println("Balance : ₹" + balance);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("Rahul Sharma", 25000);
account.displayAccount();
}
}
Sample Output
Account Holder : Rahul Sharma
Balance : ₹25000.0
Explanation
The BankAccount class stores:
- Account Holder Name
- Account Balance
The constructor initializes these values, and the displayAccount() method prints the account details.
This is a simple real-world example of object-oriented programming.
Concepts Covered
- Classes
- Objects
- Constructors
- Real-World OOP Example
11. Java Program to Demonstrate Encapsulation
Problem Statement
Write a Java program to demonstrate Encapsulation by making class variables private and accessing them using getter and setter methods.
Java Solution
class Student {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.setName("Rahul");
System.out.println("Student Name : " + student.getName());
}
}
Sample Output
Student Name : Rahul
Explanation
Encapsulation means hiding data from direct access.
Instead of accessing variables directly, we use:
- Setter Methods → Assign values
- Getter Methods → Retrieve values
This improves security and data integrity.
Concepts Covered
- Encapsulation
- Private Variables
- Getter Methods
- Setter Methods
12. Java Program to Create a Simple Car Class
Problem Statement
Write a Java program to create a Car class that stores the brand, model, and manufacturing year.
Java Solution
class Car {
String brand;
String model;
int year;
Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
void display() {
System.out.println("Brand : " + brand);
System.out.println("Model : " + model);
System.out.println("Year : " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", "Fortuner", 2024);
car.display();
}
}
Sample Output
Brand : Toyota
Model : Fortuner
Year : 2024
Explanation
The Car class represents a real-world object.
Each car object has its own:
- Brand
- Model
- Year
Concepts Covered
- Classes
- Objects
- Constructors
- Real-World OOP Example
13. Java Program to Create a Book Class
Problem Statement
Write a Java program to create a Book class that stores the title, author, and price.
Java Solution
class Book {
String title;
String author;
double price;
Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
void display() {
System.out.println("Title : " + title);
System.out.println("Author : " + author);
System.out.println("Price : ₹" + price);
}
}
public class Main {
public static void main(String[] args) {
Book book = new Book("Java Programming", "James Gosling", 699);
book.display();
}
}
Sample Output
Title : Java Programming
Author : James Gosling
Price : ₹699.0
Explanation
This program demonstrates how objects can represent books with different properties.
Each object stores its own values.
Concepts Covered
- Objects
- Constructors
- Classes
- Instance Variables
14. Java Program to Create a Product Class
Problem Statement
Write a Java program to create a Product class that stores product details.
Java Solution
class Product {
int productId;
String productName;
double price;
Product(int productId, String productName, double price) {
this.productId = productId;
this.productName = productName;
this.price = price;
}
void display() {
System.out.println("Product ID : " + productId);
System.out.println("Product Name : " + productName);
System.out.println("Price : ₹" + price);
}
}
public class Main {
public static void main(String[] args) {
Product product = new Product(101, "Laptop", 65000);
product.display();
}
}
Sample Output
Product ID : 101
Product Name : Laptop
Price : ₹65000.0
Explanation
The Product class represents products available in an inventory system.
Each object stores different product details.
Concepts Covered
- Classes
- Objects
- Constructors
- Inventory Example
15. Java Program to Create a Mobile Class
Problem Statement
Write a Java program to create a Mobile class that stores the mobile brand, model, and price.
Java Solution
class Mobile {
String brand;
String model;
double price;
Mobile(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
void display() {
System.out.println("Brand : " + brand);
System.out.println("Model : " + model);
System.out.println("Price : ₹" + price);
}
}
public class Main {
public static void main(String[] args) {
Mobile mobile = new Mobile("Samsung", "Galaxy S25", 79999);
mobile.display();
}
}
Sample Output
Brand : Samsung
Model : Galaxy S25
Price : ₹79999.0
Explanation
The Mobile class is another real-world OOP example.
It stores information related to a smartphone and displays it using an object method.
Concepts Covered
- Classes
- Objects
- Constructors
- Real-World OOP Model
Chapter Summary
In this chapter, you learned the fundamentals of Object-Oriented Programming (OOP) in Java. OOP is the backbone of Java development and is used to build scalable, reusable, and maintainable software.
You practiced creating and using:
- Classes
- Objects
- Instance Variables
- Instance Methods
- Default Constructors
- Parameterized Constructors
- Multiple Objects
- Real-world classes such as Student, Employee, Bank Account, Product, Book, Car, and Mobile
- Encapsulation using Getter and Setter methods
- The
thiskeyword for object initialization
These concepts form the foundation for advanced Java topics such as Inheritance, Polymorphism, Abstraction, Interfaces, Packages, Exception Handling, Collections Framework, and Spring Boot Development.
Understanding OOP is essential for Java interviews and professional software development because almost every Java application is designed using object-oriented principles.
Key Takeaways
- A class is a blueprint for creating objects.
- An object is an instance of a class.
- Constructors initialize objects automatically during creation.
- Java provides:
- Default Constructors
- Parameterized Constructors
- The
thiskeyword refers to the current object. - Encapsulation protects data using private variables and getter/setter methods.
- Objects can represent real-world entities such as students, employees, products, and bank accounts.
- OOP improves code reusability, modularity, and maintainability.
- Java is primarily an object-oriented programming language.
- Strong OOP knowledge is essential before learning inheritance and polymorphism.
Frequently Asked Questions (FAQs)
1. What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) is a programming paradigm that organizes software into classes and objects, making code modular, reusable, and easier to maintain.
2. What is the difference between a class and an object?
| Class | Object |
|---|---|
| Blueprint | Real Instance |
| Defines properties and methods | Uses properties and methods |
| Logical entity | Physical entity |
Example:
Student student = new Student();
Here:
Student→ Classstudent→ Object
3. What is a constructor in Java?
A constructor is a special method that is automatically executed whenever an object is created.
Example:
Student() {
System.out.println("Constructor Called");
}
4. What is the purpose of the this keyword?
The this keyword refers to the current object.
Example:
this.name = name;
It differentiates the instance variable from the constructor parameter.
5. What is Encapsulation?
Encapsulation is the process of hiding data using private variables and providing controlled access through getter and setter methods.
Example:
private String name;
6. What are the four pillars of OOP?
The four pillars of Object-Oriented Programming are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
7. Why is OOP important in Java?
OOP helps developers build:
- Reusable Code
- Modular Applications
- Secure Programs
- Scalable Software
- Maintainable Systems
It is the foundation of almost every Java application.
8. Where is OOP used in real-world applications?
Object-Oriented Programming is used in:
- Banking Software
- E-commerce Applications
- Android Apps
- Desktop Applications
- Hospital Management Systems
- Student Management Systems
- Inventory Systems
- Enterprise Applications
- Game Development
- Web Applications
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
