top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

OOPS CONCEPTS IN JAVA

Updated: Sep 5


Hello everyone!!

I would like to share my knowledge on basic OOPs concepts in Java.


Java is an Object-Oriented Programming Language. OOPs (Object Oriented Programming System) is a way of designing and structuring programs using objects (real -world entities) and classes (blueprints of objects).

Unlike procedural programming, which focuses on functions and logic, Object-Oriented Programming (OOP) focuses on objects, which are real-world entities that have state (attributes) and behavior (methods).

 

WHY OOP?

·     Structures code into logical units (classes and objects)

·     Keeps related data and methods together (encapsulation)

·     Makes code modular, reusable and scalable

·     Prevents unauthorized access to data

·     Follows the DRY (Don’t Repeat Yourself) principle


Major Components of Object-Oriented programming
Major Components of Object-Oriented programming

 

Java - What are Classes and Objects?

Let’s have a basic idea of what is meant by classes and objects in Java.


Classes and objects are basic concepts of OOPs that are used to represent real world concepts and entities.

 

A Class is a blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. We can create multiple objects with the same behavior using classes. Any class comprises of the following:

·     Modifiers- A class can be public or have default access.

·     Class Name- The class name should begin with the initial letter as capital.

·     Body- The class body is surrounded by braces, {}.

 

 

An Object is a basic unit of Object-Oriented Programming that represents real-life entities. In a Java program, we can create multiple objects of a class which interact by invoking methods. An object mainly consists of:

·     State: It is represented by the attributes of an object. It also reflects the properties of an object.

·     Behavior: It is represented by the methods of an object. 

·     Identity: It is a unique name given to an object that enables it to interact with other objects.

·     Method: A method is a collection of statements that perform some specific tasks and return the result to the caller.

 

Example:

If you want to create a class for Students. In that case “Student” will be class name and student records like Student1, Student 2 etc. will be object names.


 Here, is an example -

An example of class named Student
An example of class named Student

 Now, let’s create some students as objects and print their details. Here is an example -


Here student1, student2, student3 are objects of class Student
Here student1, student2, student3 are objects of class Student

The four main pillar of OOPs in Java are:


1.  Encapsulation

Encapsulation is the process of wrapping data (variables) and methods (functions) together into a single unit called a class. It restricts direct access to important data. Another way to think about encapsulation is  that it is a protective shield that prevents  the data from being accessed by the code outside this shield.

 

 

Real-Life Analogy:

Think of an ATM machine. You interact with buttons and a screen, but you cannot see the internal circuits and processes. The complexity is hidden, and you get only what you need--deposit, withdraw, balance inquiry.

 

 

How To Achieve?

The best example of the encapsulation concept is making a class where the data members are private, and methods are public to access through an object. This can be achieved through public getter and setter methods. So, only methods can access those private data.

 

 

 

Here is a Java example of Encapsulation,

Using getter and setter methods to achieve Encapsulation
Using getter and setter methods to achieve Encapsulation

2.  Inheritance

Inheritance is a process by which we can reuse the functionalities of existing classes to new classes. When a class is inherited from another class, it obtains all the properties and behaviors of that class. It promotes code reusability.

 

 

Real-Life Analogy:

A child inherits qualities (like eye color, language, traditions) from their parents but also has unique qualities.

 

Let us discuss some terms associated with Inheritance:

·     Superclass- The class whose features are inherited is known as superclass (also known s base class or parent class).

·     Subclass – The class that inherits the other class is known as subclass (also known as derived or extended or child class). The subclass can add its own methods and fields in addition to the superclass methods and fields.

·     Reusability- If we create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing so, we are reusing the fields and methods of the existing class.

 

How To Achieve?

Inheritance is achieved using the extends keyword.

 

Here is a Java example of Inheritance,

                                                              Here Dog is a child class of parent class Animal
Here Dog is a child class of parent class Animal

3.Polymorphism

The term “Polymorphism” means many forms. In Java, a single method, operator, or object can behave differently in different contexts.

 

Real-Life Analogy:

The word “drive” has different meanings depending on context: drive a car, drive a bike, drive a bus. Same action, different outcomes.

 

To implement polymorphism in Java, we use two concepts: Method Overloading and Method Overriding.

The method overloading is performed in the same class where we have multiple methods with the same name but different parameters, whereas the method overriding is performed by using the inheritance where we can have multiple methods with the same name in parent and child classes.


Example (Method Overloading):

Here function add is used differently in both methods
Here function add is used differently in both methods

Example: Method Overriding

Here method sound of class Dog overrides the sound method of the parent class Animal
Here method sound of class Dog overrides the sound method of the parent class Animal

4.Abstraction

Abstraction in Java is the process of hiding the implementation details and only showing the essential details or features to the user. It allows to focus on what an object does rather than hoe it does it. The unnecessary details are not displayed to the user.


Real-Life Analogy:

The real-world example of an abstraction is a Car, the internal details such as the engine, process of starting a car, process of shifting gears, etc. are hidden from the user, and features such as the start button, gears, display, break, etc. are given to the user. When we perform any action on these features, the internal process works.

 

How To Achieve?

The abstract classes and interfaces are used to achieve abstraction in Java.


Here is an example of Abstraction in Java -

Here Abstract Class Animal has abstract method used by subclasses
Here Abstract Class Animal has abstract method used by subclasses

Conclusion:

OOPs in Java provides a structured way to design and develop applications by organizing codes into classes and objects. Its core concepts such as -reusability, encapsulation, polymorphism make it suitable for large , complex and scalable projects.

However, OOPs also comes with certain drawbacks like added complexity, and slower execution in some cases but its long term benefits outweigh the drawbacks most of the time.

In short, OOPs helps developers write cleaner, more maintainable and reusable code, that is why it remains a cornerstone of modern Java development.



Happy Learning :)


Thank you for reading and please give your valuable feedback.




 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page