Object-Oriented Programming System (OOPS) is a programming paradigm built around the concept of objects — self-contained units that represent real-world entities like users, cars, or bank accounts. While objects are at the heart of OOPS, they are just one part of a broader system of concepts that define how OOP works. Together, these OOPS concepts (like classes, objects, methods, etc) concepts—form the building blocks of modular and efficient software development.
In this blog, we won’t dive into the four pillars of OOPS — Abstraction, Encapsulation, Polymorphism, and Inheritance — just yet. Instead, we’ll focus on some of the foundational Java concepts that are essential for understanding OOPS.
These include objects, classes, constructors, methods, access modifiers, etc — the building blocks of any Java program and the building blocks of OOPS as the four pillars use them to make our code modular, maintainable and flexible. Let’s revisit these concepts with a practical mindset and some real-world examples to see how they truly work
Let’s start…
Index
Different OOPS Concepts in Java
As mentioned earlier, in this blog we’ll focus on some of the core concepts of Object-Oriented Programming (OOP), which are essential for writing clean and effective code in Java. Without a solid understanding of these OOPS concepts, it’s hard to fully appreciate the power of OOP and use it to make our code modular, flexible, and secure.
In this post, we’ll concentrate on five key concepts: classes, objects, methods, access modifiers, and constructors. While there are many other important OOP concepts, we’ve chosen these because they are especially relevant for beginners for will help them understand OOP better. The more advanced topics will be covered in future posts, as they require a deeper conceptual understanding.
Among these five, we’ll be creating separate blog posts for constructors and access modifiers, since they deserve a more detailed explanation. Understanding them is crucial both from a conceptual standpoint and for practical coding in Java.
Let’s start…
Class
A class is a blueprint of an object or rather a blueprint that is used to create an object as it tells us which properties and methods will that object or objects of similar kind have. As Java likes to work on the DRY principle.
Writing code once and using it multiple times in short is the DRY principle and it makes our program scalable, maintainable, and easier to understand. This is where classes play a crucial role. With classes, we can create many objects that share the same behavior, instead of writing the same code over and over again.
Syntax
[access modifier] class [ClassName] {
Body …..
}
Here we have 3 components –
- Access Modifier – Keywords that control the visibility and accessibility of classes.
- ClassName – The name given to the class we have created. Java follows the PascalCase convention for naming classes
- Body – The body of the class is a block of code enclosed in {} where we define the class’s members, constructors, and methods.
Object
An object is an instance of a class and the basic entity of Object Oriented Programing. It’s a physical entity that contains real values instead of variables. It comprises two things – state and behavior.
State refers to the current values or properties of an object — like its color, size, or name. These values can change when the object interacts with other objects or when methods are called.
Behavior defines what an object can do or how it responds to actions, like walking, talking, or moving, especially when it interacts with other objects.
Let’s take a dog as an example to understand these concepts. In the case of a dog, its state will be its name, breed, color, age, etc things while its behavior will be barking, eating, wagging his tail, etc.
Objects are the basic entities of OOPS which are initialized as soon as they are created. They can be created as many times as possible which unlike classes, consumes memory.
Syntax
ClassName Object_Name = new ClassName([parameters]);
When we use the new keyword to create an object (as shown above), it tells the Java Virtual Machine (JVM) to set aside memory for that object. This means the new keyword creates a fresh object instance based on a class blueprint in the computer’s memory.
Benefits of using Objects
- Modularity – The source code of one object is written and maintained separately from other objects. But once an object is created, it can be called as many times as possible inside the system.
- Data Protection – As all the interactions are done by the methods of the object, the actual and important details of the object like the implementation process gets hidden from the outside world thus ensuring its safety.
- Reusability – As we’ve discussed, Java follows the DRY (Don’t Repeat Yourself) principle. Once an object is written, it can be reused multiple times in different parts of the program, saving time and reducing redundancy.
- Maintainability – A piece of code is considered maintainable when small changes can fix issues without affecting the entire system. Let’s take a real-world example of an electronic device that runs on batteries. If it stops working and we discover that replacing the old batteries brings it back to life, we don’t need to open up the entire device or understand its inner workings — we just replace the batteries, and it works again.
Similarly, in programming, if a specific object is causing a problem, we can simply replace it with another object that works correctly, without having to rewrite or understand the entire code base.
Writing code in a way that allows for small, isolated fixes, without breaking the rest of the system, is what we call maintainable and modular code. It’s like designing your program with replaceable parts
Difference Between Class and Object
Feature | Class | Object |
Definition | A blueprint or template for creating objects. | An instance of a class. |
Purpose | Defines properties (attributes) and behaviors (methods). | Represents a real-world entity with state and behavior. |
Memory Allocation | No memory is allocated when a class is defined. | Memory is allocated when an object is created. |
Usage | Used to group related data and functions together. | Used to access and manipulate data defined in the class. |
Declaration | Declared using keywords like class in languages like Java, Python. | Created using the new keyword or directly calling the class in Python. |
Example | public class Car {} | Car myCar = new Car() |
Number | It can be used to create multiple objects. | Each object is a separate instance of a class. |
Type | Logical construct. | Physical entity in memory. |
Reusability | Can be reused to create multiple objects. | Each object holds its own data but shares class functionality. |
Naming Convention | PascalCase (eg – MyCar) | camelCase (eg – myCar) |
Methods
Actions or functions performed by the objects within a class that represent their behavior are called methods. They are used to perform operations, access and modify object data, and interact with other objects.
Methods allow us to reuse the code without retyping it, which is why they are considered time savers.
Method Overloading
A process that occurs when multiple methods in the same class have the same name but different parameter lists (different number or types of parameters). It is an example of compile-time polymorphism.
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
double add(int a, double b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // Output: 5
System.out.println(calc.add(2, 3, 4)); // Output: 9
System.out.println(calc.add(2, 3.5, 4)); // Output: 9.5
}
}
The above code is the simplest and easiest example for understanding method overloading. Now in the above example we have three methods that do the exact same thing – addition of numbers, here numbers can be either whole numbers or fractional numbers as already shown in the code.
So we have three functions with the same name but with different parameters or types of parameters, which is the definition of method overloading
Method Overriding
A process where a subclass provides its own version of a method that already exists in its parent class, allowing it to replace the parent’s class version only if that version has the same class name, return type, and parameters.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Output: Dog barks
}
}
Just like the above example of method overloading, this is also the simplest and easiest example to understand method overriding. If we see the initial sound method and the output the code gives us, it’s clear that somewhere in the middle the result was changed or replaced with something other than the original text. This is method overriding.
Changing the output of a method from what the parent class originally provided to a new version defined in the child class is called method overriding.
It’s like the parent class put a default behavior on the table, but the child class decided to replace it with its own. And as long as the method name and parameters stay the same, this new version overrides the old one at runtime.
We will deeply understand these concepts while understanding Compile-time and Run-time Polymorphism.
Subclass and Super class
We will constantly encounter these two terms in OOPS as they play a very crucial role in understanding the code and differentiating classes and at some point we developers found ourselves dealing with multiple classes at the same time and if we have to explain our code to someone else these terminologies might come in handy. They are also useful in many ways other than the one explained above like maintaining, debugging, etc things.
Well for starters we will not be using these technical terms in blogs or rather we will keep their use to a minimum as we all from diverse backgrounds using them without understanding them is a no go. So let’s understand these terms.
FYI, we have used these terms in the blog but in a different way, and we will be using those terms only in our future blogs unless required for the technical ones
Subclass or Child Class
A type of class that inherits properties and methods from its parent class (Super class) or any other class is called the subclass or the child class.
Super class or Parent Class
A type of class from which other classes inherit properties and methods is called a super class or the parent class.
Now that we have understood what super class and subclass are from the theory point of view, let’s also understand it from the practical point of view for a better and deeper understanding.
The above image shows the bond between three classes – the ancestor class is directly linked to the parent class and the parent class is directly linked to the child class, this type of system is called single inheritance (we will understand inheritance in upcoming blogs).
Now, let’s understand one thing about both the super class and subclass. Any class can become a subclass or a super class depending upon whether it is inheriting or the one being inherited from, i.e, whether that class is acting as a donor or receiver.
This explanation might be confusing at first, but see the above image again, especially the arrows. Let’s say that the relation between the ancestor class and the parent class is of a parent and a child, so here the ancestor class is the super class – the donor and the Parent class is the subclass – the receiver. Parent class will inherit or receive some or all the properties of the Ancestor class. And the same thing is true for the Parent and the Child class
Constructors
In Java, constructors are special methods used to create and initialize objects. Unlike regular methods, constructors have no return type and share the same name as the class and are created as soon as an object is created using the new keyword. They are of 3 types – Default, No argument, and Parameterized Constructors.
Syntax
[Access Modifier] ClassName (Parameters) {
Body…
}
class Example {
// This is a constructor
Example() {
System.out.println("This is a constructor");
}
// This is a method, not a constructor
void Example() {
System.out.println("This is a method");
}
}
When we said that a constructor is a special type of method, we meant that it’s a method with no return type, not even void. If you define a method with a return type such as int, float, or void, even if it has the same name as the class, the compiler will not treat it as a constructor. Instead, it will simply be considered a regular method.
Access Modifiers
Access modifiers in Java are keywords used to define the visibility and accessibility of classes, methods, variables, and constructors. They play a crucial role in encapsulation, allowing developers to control how the components of a class can be accessed from other classes. Java provides four primary access modifiers: They are of four types – Public, Private, Default, and Protected.
Understanding these OOPS Concepts through a simple example
A code in Java comprises all the above-discussed parts – class, object, method, constructor, and abstract modifier. Let’s understand their use through an example code of a 3-year-old white Pomeranian dog named Dollar.
public class Dog {
// STATE (attributes)
String name;
String breed;
String color;
int age;
// Constructor to initialize the dog's state
public Dog(String dogName, String dogBreed, String dogColor, int dogAge) {
name = dogName;
breed = dogBreed;
color = dogColor;
age = dogAge;
}
// BEHAVIOR (methods)
public void bark() {
System.out.println(name + " says: Woof!");
}
public void sit() {
System.out.println(name + " is now sitting.");
}
public void eat() {
System.out.println(name + " is eating.");
}
// Main method to run the program
public static void main(String[] args) {
// Create a Dog object
Dog myDog = new Dog("Dollar", "Pomeranian", "White", 3);
// Access state
System.out.println("Name: " + myDog.name);
System.out.println("Color: " + myDog.color);
// Call behavior methods
myDog.bark();
myDog.sit();
myDog.eat();
}
}
Output
Let’s break it down and understand each part separately.
Class Creation
Firstly, we have created a class using the class syntax
public class Dog {}
Here, public is the access modifier telling us that this class is visible and accessible to everyone. and Dog is the ClassName. Through this line we are telling the JVM that we have created a class named Dog which can be accessed by everyone as we have used the public access modifier.
Object Creation
// Create a Dog object
Dog myDog = new Dog("Dollar", "Pomeranian", “White", 3);
If we compare the above code with the syntax of object creation –
- Dog -> ClassName
- myDog -> Object_Name
- new -> keyword necessary for object creation
- Dog – > ClassName
- (“ “) -> Parameters
These parameters are state or attributes of an object, which are initialised using a constructor
We know from above paragraphs that an object comprises state and behavior. States, we have already seen, are used as parameters while the behaviors as methods which allow the object to interact with other objects.
Declaring State Variables
String name;
String breed;
String color;
int age;
We have four states as the example is about a white colored Pomeranian dog named dollar and is 3 years old. Here we are telling four attributes of a dog – name, color, age and breed thus the states.
The other thing that object has are the methods or behaviors
Method Declaration
Behaviors are actions that the objects perform like interacting with other objects. In this example behaviors are the actions that a dog performs like “Woof”, sit and eat.
public void bark() {
System.out.println(name + " says: Woof!");
}
public void sit() {
System.out.println(name + " is now sitting.");
}
public void eat() {
System.out.println(name + " is eating.");
}
Now, whenever an object is created a constructor is called as it lets us initialise some values to the object.
Constructor Creation
As told above, constructors are special types of methods in Java that
- Are used to initialize objects
- Are created as soon as an object is created using the new keyword
- Do not have a return type
- Has the same name as the class
In the above example
public Dog(String dogName, String dogBreed, String dogColor, int dogAge) {
name = dogName;
breed = dogBreed;
color = dogColor;
age = dogAge;
}
If we compare this with the syntax
- public: This is an access modifier, it means the constructor is visible and can be accessed from outside the class.
- Dog: This is the constructor name, which must match the class name exactly. This is how Java knows it’s a constructor and not a regular method.
- String dogName, String dogBreed, String dogColor, int dogAge: These are parameters passed to the constructor. They allow the initialization of object properties when it is created.
- name = dogName – this and other like this are a part of the constructor’s body which assigns the parameter value to the instance variable.
The Main Method
This is the main part of the code as it tells the JVM where to begin execution of the program. So, if a JAVA program does not have a main method, it will not be executed, instead will throw an error saying
“can't find the main(String[] args) method”.
Read this blog to get an in-depth understanding of this method,
The main method of our Dog class goes something like this
public static void main(String[] args) {
Dog myDog = new Dog("Dollar", "Pomeranian", "White", 3);
// Access state
System.out.println("Name: " + myDog.name);
System.out.println("Color: " + myDog.color);
// Call behavior methods
myDog.bark();
myDog.sit();
myDog.eat();
}
Let’s just understand each part in brief, as a lot is going on in a few lines. We understood the gist of the main method – why it is important in a Java program. The main method is more than just main(String[] args). It also includes
- A Public Access Modifier.
- A Static Keyword that tells the JVM that this line belongs to the class and not the object.
- A return Type of Void, which tells the JVM that there will be no return type.
- A Main Keyword that tells JVM that this is the entry point of the program.
- An array of type java.lang.String class, which is used to store the Java Command Line arguments – String[] args.
When combined, it tells the JVM that this method is the starting point of the program, which belongs to the class only and can be accessed by anyone from inside or outside the Dog class.
Now coming to the next part – Object Creation which we have already discussed above which is followed by us calling the states and methods which we have created using the object.
Conclusion
In this blog we understood many things which are not directly linked to OOPS but to the Java programming language itself and as we understand OOPS in Java it is very important to also understand these concepts in order to understand concepts of OOPS well.
These concepts are – Classes, Objects, Methods, Constructors and Access Modifiers. Well there are plenty more concepts that are still left untouched, but be sure we will cover them as they come in our upcoming blogs.
Frequently Asked Questions
Q1. What is OOPS in programming?
Ans. OOPS stands for Object-Oriented Programming System, a paradigm that organizes software design around objects representing real-world entities.
Q2. Why is OOPS important in software development?
Ans. OOPS helps create modular, reusable, and maintainable code, making software easier to develop and scale.
Q3. What are constructors in Java?
Ans. Constructors are special methods used to initialize objects when they are created.
Q4. What are some commonly used OOP languages?
Ans. C++, Java, Python, C#, JavaScript and Ruby
Q5. What are Access Methods and why are they used in OOPS?
Ans. Access Modifiers are special types of keywords in Java that are used to control the accessibility and visibility entities like classes, methods, and so on. Private, Public, Protected and default are different types of access modifiers.|
They are mainly used in abstraction and encapsulation as they increase the security of the code by restricting the accessibility of the code.