OOPS Concepts 101: What Are the Key Concepts You Need to Know?

Share your love

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

Different OOPS Concepts

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 – 

  1. Access Modifier – Keywords that control the visibility and accessibility of classes.
  2. ClassName – The name given to the class we have created. Java follows the PascalCase convention for naming classes 
  3. Body – The body of the class is a block of code enclosed in {} where we define the class’s members, constructors, and methods.

Object

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

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

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.

Understanding Sub Classes and Super Classes

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");
    }
}

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

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 –

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

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 

In the above example 

public Dog(String dogName, String dogBreed, String dogColor, int dogAge) {
    name = dogName;
    breed = dogBreed;
    color = dogColor;
    age = dogAge;
}

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”.
Error message

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

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?

Q2. Why is OOPS important in software development?

Q3. What are constructors in Java?

Q4. What are some commonly used OOP languages?

Q5. What are Access Methods and why are they used in OOPS?

Share your love