Sunday, September 7, 2025

Java - OOPs (Object-Oriented Programming) Concepts

 OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. Object means a real-world entity such as a mobile, book, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug. (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
  • OOP makes it possible to create full reusable applications with less code and shorter development time.

Java OOPs (Object-Oriented Programming) Concepts are ;

  • Class

In object-oriented programming, In Java, a class serves as a blueprint  (design plan) or template for objects. It defines the structure and behavior that objects of that class will possess. In Java, everything is related to classes and objects. Each class has its methods and attributes that can be accessed and manipulated through the objects.
  • Object

    In object-oriented programming, an object is an entity that has two characteristics (states and behavior). Some of the real-world objects are book, mobile, table, computer, etc. An object is a variable of the type class, it is a basic component of an object-oriented programming system. A class has the methods and data members (attributes), these methods and data members are accessed through an object. Thus, an object is an instance of a class.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

  • Inheritance

In Java, inheritance is a process by which we can reuse the functionalities of existing classes to new classes.It inherits   attributes and methods from one class to another. We group the "inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword. For example, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):

  Java does not support multiple inheritance for classes, whether they are concrete or abstract. This design decision is primarily to avoid complexities like the "Diamond Problem," where ambiguities can arise if a class inherits the same method signature from multiple parent classes with different implementations. 

However, a class in Java can implement multiple interfaces, which can contain abstract methods. This allows for achieving a form of multiple inheritance of behavior. Additionally, you can create a chain of inheritance where a class extends one abstract class, and that abstract class in turn extends another abstract class, and so on. This provides an indirect way to inherit functionalities from multiple abstract classes.

  • Polymorphism

The term "polymorphism" means "many forms". In object-oriented programming, polymorphism is useful when you want to create multiple forms with the same name of a single entity. To implement polymorphism in Java, we use two concepts method overloading and method overriding.

 It allows objects to take on multiple forms and enables a single action to be performed in different ways. In essence, it allows you to treat objects of different classes in a uniform manner, provided they share a common superclass or interface.

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.

  • Abstraction

Abstraction in Java is a core concept of Object-Oriented Programming (OOP) that focuses on showing only essential information to the user while hiding the complex implementation details. It allows for simplifying complex systems and improving code maintainability and flexibility.

Key aspects of Abstraction in Java:

Hiding Implementation Details: Abstraction focuses on "what" an object does rather than "how" it does it. Users interact with a simplified interface without needing to understand the underlying complexity.

Achieved through Abstract Classes and Interfaces:

Abstract Classes: These are classes declared with the abstract keyword. They can contain both abstract methods (methods without a body) and concrete methods (methods with a body). Abstract classes cannot be instantiated directly; they must be extended by a concrete subclass which provides implementations for all abstract methods.

Interfaces: These are blueprints of a class that can contain abstract methods (before Java 8) and default or static methods (from Java 8 onwards). A class implements an interface using the implements keyword and must provide implementations for all abstract methods declared in the interface. Interfaces achieve 100% abstraction (for abstract methods).

Benefits of Abstraction:

Reduced Complexity: Simplifies the system by presenting a high-level view and hiding intricate details.

Improved Maintainability: Changes to the implementation details of an abstract method or interface method do not affect the client code as long as the method signature remains the same.

Enhanced Flexibility: Allows for different implementations of the same abstract behavior, promoting polymorphism.

Increased Security: Prevents direct access to sensitive internal working

 

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.

The abstract keyword is a non-access modifier, used for classes and methods:

    • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

    • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}

 

  • Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding "sensitive" data is hidden from users. To achieve this, you must:

    • declare class variables/attributes as private
    • provide public get and set methods to access and update the value of a private variable


References used :- OOPs (Object-Oriented Programming System) ,  w3schools - Java

No comments:

Post a Comment

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...