Skip to main content

Object Oriented Programming (OOPs) Concept

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of "Classes and Objects."

A class is a blueprint or template that defines the structure and behavior of objects. It contains data members (fields) and methods that operate on the data.

An object is an instance of a class, representing a specific entity in the real world. Objects have state (values of their attributes) and behavior (actions they can perform).

Classes serve as a blueprint for creating objects, and objects interact with each other through method calls. Here is an example of a class and object:

In Java, OOP is a fundamental approach that allows you to model real-world entities as objects and design software with modularity, reusability, and maintainability. There are four primary OOP concepts in Java:

1) Inheritance

Inheritance allows a class (subclass/derived class) to inherit properties and behaviors from another class (superclass/base class).

It promotes code reusability and supports the "is-a" relationship between classes.

The subclass extends the superclass using the "extends" keyword, and it can override superclass methods to provide specialized behavior.

2) Polymorphism

Polymorphism allows a class to take on multiple forms, typically through method overriding and method overloading.


2.1) Method Overloading

Method overloading is the ability to define multiple methods with the same name in the same class but with different parameter lists.

The methods must have different numbers or types of parameters. The return type may be the same or different, but it alone does not determine method overloading.

The compiler determines which version of the method to call based on the number and types of arguments passed during the method invocation.

2.2) Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

The method signature (name, return type, and parameters) must be the same in both the superclass and the subclass.

The purpose of method overriding is to provide a specialized implementation of the method in the subclass, which promotes the "dynamic method dispatch" feature of polymorphism.

When an overridden method is called on an object of the subclass, the subclass's version of the method is executed, even if the reference to the object is of the superclass type.

In the example above, the Dog class overrides the makeSound() method from the Animal class, providing a specific implementation for the sound.

3) Abstraction

Abstraction allows you to hide the implementation details of an object while exposing only the essential characteristics and behaviors.

It enables you to create more manageable and understandable code by focusing on what an object does rather than how it does it.

In Java, abstraction is achieved through abstract classes and interfaces:


3.1) Abstract Classes

An abstract class is a class that cannot be instantiated directly; you cannot create objects of an abstract class.

It serves as a blueprint for other classes, allowing them to inherit its properties and methods.

Abstract classes can have both abstract (methods without a body) and concrete (methods with a body) methods.

Any class that extends an abstract class must provide implementations for all its abstract methods or be declared abstract itself.

3.2) Interfaces

An interface is a collection of abstract methods and constant variables (constants are implicitly declared as public, static, and final).

Interfaces allow classes to implement multiple behaviors without multiple inheritance conflicts.

A class implements an interface using the implements keyword and must provide concrete implementations for all the interface's methods.

4) Encapsulation

Encapsulation is the process of bundling data (attributes) and methods that operate on that data within a single unit (class).

The data members are typically declared as private to hide the internal state of an object from the outside world.

Public methods (getters and setters) are used to access and modify the object's state, enforcing controlled access to the object's data.

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...