codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

24 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java OOP
☕Java Programming

Java OOP

Updated 2026-05-12
30 min read

Java OOP

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications. It emphasizes encapsulation, inheritance, and polymorphism, making code more modular, reusable, and easier to maintain. Understanding classes and objects is the foundation of OOP in Java.

Introduction

In this tutorial, we'll explore the core concepts of Object-Oriented Programming (OOP) in Java, focusing on classes and objects. We'll see how these building blocks help us create more organized and efficient code. By the end of this section, you'll be able to define your own classes, create objects, and understand their relationships.

Core Content

What are Classes and Objects?

  • Class: A blueprint for creating objects. It defines a set of properties (attributes) and methods (functions) that the created objects will have.
  • Object: An instance of a class. When a class is defined, no memory is allocated until an object is created.

Think of a class as a template for making cookies. The recipe (class) describes how to make the cookie, including its ingredients and shape. Each cookie made from this recipe (object) has the same basic structure but can have unique characteristics like different flavors or decorations.

Creating a Class

Let's start by creating a simple class in Java. We'll define a Car class with some attributes and methods.

Car.java
1public class Car {
2 // Attributes
3 String make;
4 String model;
5 int year;
6
7 // Constructor
8 public Car(String make, String model, int year) {
9 this.make = make;
10 this.model = model;
11 this.year = year;
12 };
13
14 // Method to display car details
15 public void displayInfo() {
16 System.out.println("Car: " + make + " " + model + ", Year: " + year);
17 }
18}

Creating an Object

Now that we have a Car class, we can create objects based on this class.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 // Create Car objects
4 Car myCar = new Car("Toyota", "Corolla", 2020);
5 Car friendCar = new Car("Honda", "Civic", 2019);
6
7 // Display car information
8 myCar.displayInfo();
9 friendCar.displayInfo();
10 }
11}
Output
Car: Toyota Corolla, Year: 2020
Car: Honda Civic, Year: 2019

Understanding Constructors

A constructor is a special method that is called when an object is created. It initializes the object's attributes.

In the Car class example, we defined a constructor that takes three parameters (make, model, and year) to initialize these attributes when a new Car object is created.

Access Modifiers

Access modifiers control the visibility of classes, methods, and variables. The most common access modifiers in Java are:

  • public: Accessible from any other class.
  • private: Accessible only within the same class.
  • protected: Accessible within the same package and subclasses.
  • (no modifier): Default access, accessible only within the same package.
Car.java
1public class Car {
2 // Private attributes
3 private String make;
4 private String model;
5 private int year;
6
7 // Constructor
8 public Car(String make, String model, int year) {
9 this.make = make;
10 this.model = model;
11 this.year = year;
12 }
13
14 // Public method to display car details
15 public void displayInfo() {
16 System.out.println("Car: " + make + " " + model + ", Year: " + year);
17 }
18}

Encapsulation

Encapsulation is the practice of hiding the internal state of an object and requiring all interaction to be performed through an object's methods. This is achieved using private access modifiers for attributes and public getter/setter methods.

Car.java
1public class Car {
2 // Private attributes
3 private String make;
4 private String model;
5 private int year;
6
7 // Constructor
8 public Car(String make, String model, int year) {
9 this.make = make;
10 this.model = model;
11 this.year = year;
12 }
13
14 // Getter and Setter for make
15 public String getMake() {
16 return make;
17 }
18
19 public void setMake(String make) {
20 this.make = make;
21 }
22
23 // Getter and Setter for model
24 public String getModel() {
25 return model;
26 }
27
28 public void setModel(String model) {
29 this.model = model;
30 }
31
32 // Getter and Setter for year
33 public int getYear() {
34 return year;
35 }
36
37 public void setYear(int year) {
38 this.year = year;
39 }
40
41 // Public method to display car details
42 public void displayInfo() {
43 System.out.println("Car: " + make + " " + model + ", Year: " + year);
44 }
45}

Practical Example

Let's create a more comprehensive example that includes encapsulation and additional methods.

Car.java
1public class Car {
2 // Private attributes
3 private String make;
4 private String model;
5 private int year;
6
7 // Constructor
8 public Car(String make, String model, int year) {
9 this.make = make;
10 this.model = model;
11 this.year = year;
12 }
13
14 // Getter and Setter for make
15 public String getMake() {
16 return make;
17 }
18
19 public void setMake(String make) {
20 this.make = make;
21 }
22
23 // Getter and Setter for model
24 public String getModel() {
25 return model;
26 }
27
28 public void setModel(String model) {
29 this.model = model;
30 }
31
32 // Getter and Setter for year
33 public int getYear() {
34 return year;
35 }
36
37 public void setYear(int year) {
38 this.year = year;
39 }
40
41 // Public method to display car details
42 public void displayInfo() {
43 System.out.println("Car: " + make + " " + model + ", Year: " + year);
44 }
45
46 // Method to start the car
47 public void start() {
48 System.out.println(make + " " + model + " is starting.");
49 }
50
51 // Method to stop the car
52 public void stop() {
53 System.out.println(make + " " + model + " is stopping.");
54 }
55}
Main.java
1public class Main {
2 public static void main(String[] args) {
3 // Create Car objects
4 Car myCar = new Car("Toyota", "Corolla", 2020);
5 Car friendCar = new Car("Honda", "Civic", 2019);
6
7 // Display car information and perform actions
8 myCar.displayInfo();
9 myCar.start();
10 myCar.stop();
11
12 friendCar.displayInfo();
13 friendCar.start();
14 friendCar.stop();
15 }
16}
Output
Car: Toyota Corolla, Year: 2020
Toyota Corolla is starting.
Toyota Corolla is stopping.
Car: Honda Civic, Year: 2019
Honda Civic is starting.
Honda Civic is stopping.

Summary

ConceptDescription
ClassA blueprint for creating objects. Defines attributes and methods.
ObjectAn instance of a class. Represents real-world entities.
ConstructorA special method to initialize object attributes.
Access ModifiersControl the visibility of classes, methods, and variables.
EncapsulationHiding internal state and requiring interaction through methods.

What's Next?

In the next section, we'll delve deeper into OOP concepts such as inheritance and polymorphism. These principles will help you create more complex and flexible applications. Stay tuned!


PreviousJava RecursionNext Java Classes/Objects

Recommended Gear

Java RecursionJava Classes/Objects