top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

JAVA OOPS (OR) OBJECT-ORIENTED PROGRAMMING CONCEPTS

ree


WHAT IS JAVA AND WHAT IS OOPS?


What is Java

 Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is widely used for developing various types of applications, from web and mobile apps to enterprise-level software.


What is OOPs (Object-Oriented Programming System)

OOPs (Object-Oriented Programming System) Object means real-world entity such as a book, mobile, table, computer, watch, tv etc, Object-Oriented Program is a programming paradigm that structures code using objects and classes. It helps in designing scalable, reusable, and maintainable software applications.


Java OOPs (Object-Oriented Programming) Concepts

  • Class

  • Object

Here the four Pillars of oops Concepts

  • Inheritance

  • Abstraction

  • Encapsulation

  • Polymorphism

Java - What are Classes and Objects

Class

A class is a blueprint from which individual objects are created (or, we can say a class is a datatype of an object type). In Java, everything is related to classes and objects. Each class has its methods and attributes(or)variables that can be accessed and manipulated through the objects.

For example, if you want to create a class for students. In that case, "Student" will be a class, and student records (like student1, student2, etc) will be objects.

Create a Class

  class student {

   Int x=5;

Public void addition() {

}

}


Object

  object is an instance of a class that has a state (attributes) and behavior (methods). It is created from a          class,which acts as a blueprint or template.


State (Attributes) (variables) – Represented by instance variables (fields).

Behavior (Methods) – Defined by functions within the class.

Identity – Each object has a unique identity in memory.


public class student {

String female;

  int score;


//method to display


Public void display(){

}

} 

public static void main(String[] args) {

 student myObj = new student(); //object creation

  myobj.display() // Calling method an object//variables or methods we can defined

}

Some Examples of Class and Objects

  Class                                                          Objects

     Fruit                                                             Apple

                                                                             Banana

Another Example

     Class                                                                  Objects

       Car                                                                       Volvo

                                                                                     Honda

                                                                                   Toyota


Abstraction

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. In Java programming, abstraction is achieved using Abstract classes and interfaces.


Example of Abstraction class

A driver will focus on the car functionality (Start/Stop -> Accelerate/ Break), he/she does not bother about how the Accelerate/ brake mechanism works internally. And this is how the abstraction works.


Another Example

phone and laptops, user can see required things like required Apps ( google, youtube, facebook) and hide the background details like phone gb, battery capacity.


ree

// Abstract class

abstract public class Vehicle {

    // Abstract method 

    Abstract  void start();

        //Here non implement the abstract method


      void stop() {

        System.out.println("Vehicle is stopping..."); //implement the abstract method

    }

}


above Abstract class having one abstract method that class should be declare as a abstract keyword.

and there is Non-implement method and implement method we can declare both and in this class we can't create an object directly. so we can create one more class that is subclass.

—----------------------------------------------------------------------


// Here one more class that is sub class

// Subclass: Bike

Public class Bike extends Vehicle {

  @Override

  void start() {

        System.out.println("Bike is starting with a kick...");

    }



// Main class


    public static void main(String[] args) {

        Bike myCar = new Bike();

         myCar.stop();  // Output: Vehicle is stopping…

         mycar.start(); // Output: Bike is starting with a kick…

  }

}  

In subclass the bike class extends to abstract class vehicle, If any class Extends to abstract class that class should be override all abstract methods, otherwise that class shoeing compile time error.

Here Extend class we can create an object.


Interface

   An interface is a completely "abstract class" that is used to group related methods with empty bodies:

here we are using implement keyword, vehicle and vehicle implement to car, bike, scooter.


ree

Example

//interface class


Public interface class Vehicle {


     public void start();

                                                     //This is empty method body

 public void stop() 

       }


To access the interface methods, the interface must be "implemented" ( like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class.


Here one more example for interface class:


Public class Bike implement Vehicle {


   public void start() {

        System.out.println("Bike is starting with a kick...");

    }


    Public void stop(){

       System.out.println("Bike is stopping with a brakes...");

    }


// Main class


    public static void main(String[] args) {

        Bike myCar = new Bike();

         myCar.stop();  // Output: Vehicle is stopping…

          myCar.start(); // Output: Bike is starting with a kick…

  }

}  


above example Bike implement to vehicle. so Abstract class we can use Extends keyword and Interface class is implement keyword.


Encapsulation

Encapsulation is a process of binding the data members (attributes) and member functions together. It restricts direct access to important data.

The best example of the encapsulation concept is making the data members of a class private, and methods as public to access through an object. In this case, only methods can access private data.

  • declare class variables/attributes as private

  • Public getter and setter methods are provided to access and update the value of private fields.

The get method returns the variable value, and the set method sets the value.


ree

above diagram, In this class Attributes and methods together inside of a class and object creation, and variables is private and get and setter methods is public.


Example

A capsule which is mixed of several medicines, the medicine are hidden data to the end users, so data is private so other class can't be access the data.


Encapsulation Example


public class Person {

  private String name;  // private = restricted access


 // Getter

  public String getName() {

    return name;

  }


// Setter

  public void setName(String newName) {

    this.name = newName;

  }

// (main method)


  public static void main(String[] args) {

    Person myObj = new Person(); //object creation

    myObj.name = "John";  // output 

    myObj.getName();                                                //output  john

    System.out.println(myObj.getName());

 }}


In this above example the class String is datatype and name is variable(String name) in variable is declare private and here get method is public, the get method return the value of the variable name and set method tales a parameter(String newName) and set he name, and object creation of person class.


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 (base class/Super class), it (derived class/Sub class) obtains all the properties and behaviors of the base class. Inheritance in Java is implemented using extends keyword.

  • subclass (child) - the class that inherits from another class

  • superclass (parent) - the class being inherited from

    Example: A child inheritance from they parents

    Here the animal Extends Dog(Dog is the subclass(child) and

    Animal is the superclass(parent).



ree

Example of Inheritance

// base class or parent class or super class


Public class Animal {

// parent class method

     public  void eat() {

     System.out.println("Animal is eating");  }

   }

—----------------------------------------------------------

//child class or subclass(one more class)


Public class Dog extends Animal {

       // child class method

     Public void bark() {   // Inherits parent class methods and they own method


      System.out.println("Dog is barking");  }

  }



     public static void main(String[] args) {

                        Dog myDog = new Dog();

                        myDog.eat(); // Inherited from Animal // Animal is eating

                myDog.bark(); // Specific to Dog   // Dog is barking

  } }


Output

Animal is eating

Dog is barking


Polymorphism

The term "polymorphism" means "many forms". In object-oriented programming, It allows an object to take multiple forms with a single entity and different type of action using “extends” keyword.


Real-life Example:

A delivery person delivers items to the user. If it’s a postman he will deliver the letters. If it’s a food delivery boy he will deliver the foods to the user. Like this polymorphism implemented different ways for the delivery function.


Example

Class Delivery Boy {

 public void deliver() {

        System.out.println("Delivering Item");

    }

 class Postman extends Delivery Boy {

    @Override

    public void deliver() {

        System.out.println("Delivering Letters");

    }

}


 class FoodDeliveryBoy extends Delivery Boy {

    @Override

    public void deliver() {

        System.out.println("Delivering Food");

    }


public static void main(String[] args) {

 // Creating objects and demonstrating polymorphism

      DeliveryBoy myDelivery = new DeliveryBoy();

         myDelivery.deliver();  // Output: Delivering Item

        DeliveryBoy myPostman = new Postman();

        myPostman.deliver();   // Output: Delivering Letters

       DeliveryBoy myFoodDeliveryBoy = new FoodDeliveryBoy();

        myFoodDeliveryBoy.deliver(); // Output: Delivering Food  }  }



Types of Polymorphism

Polymorphism in Java is mainly of 2 types as mentioned below: 

  1. Method Overloading(Compile Time Polymorphism/static)

  2.  Method Overriding(Runtime Polymorphism/dynamic)


ree

Method Overloading

Method overloading is a form of compile-time polymorphism where multiple methods in the same class share the same name but have different parameters (signatures) and return type may or may not same.


Method Overriding

Method overriding is a form of runtime polymorphism where a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.


ree

Advantages and Disadvantages of OOPs

Advantages Disadvantages

Oops provided Enhanced code reusability . The programmer should be well-skilled and should have excellent thinking in terms of objects as everything is treated as an object in oops.

The code is easier to maintain and update. Proper planning is required because oops is little bit tricky.

It provides better data security by restricting data

access and avoiding unnecessary exposure. oops concept is not suitable for all kinds of problems.



Fast to implement and easy to redesign resulting The length of the programs is much large in in minimizing the complexity of an overall program. comparision to the procedural approach.






 
 

+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