What are OOP Concepts ?


 OOPs Concepts in Java | With Examples


What exactly is OOPS?

           The Object-Oriented Programming System (OOPs) is a programming concept based on abstraction, encapsulation, inheritance, and polymorphism principles. It allows users to create the objects they want as well as the methods to interact with those objects. The basic concept of OOPs is to create objects, reuse them throughout the program, and manipulate these objects to achieve results.

OOP (Object-Oriented Programming) is a well-known and widely used concept in modern programming languages like Java.

        We'll go over these four fundamental principles and what they entail in greater depth below:

      1. Encapsulation

      2. Abstraction

      3. Inheritance

      4. Polymorphism

1. Encapsulation in Java

    Encapsulation in Java refers to the process of combining data and matching methods (behavior) into a

 single unit. Encapsulation is a programming technique that connects class members (variables and

 methods) and prevents other classes from accessing them. As a result, we can protect variables and

 methods from outside interference and misuse.


    We declare fields in the class as private to prevent other classes from directly accessing them via the encapsulation technique. The required encapsulated data can be retrieved using public Java getter and setter methods.

When a field in a class is marked as private, it cannot be accessed by anyone outside of the class, effectively hiding the field within the class. As a result, it is also referred to as data concealment.

Realtime Example of Encapsulation 

Example 1:

When you log in to your email accounts, various internal procedures take place in the background over which you have no control. When you log in, your password is encrypted and validated before you are granted access to your account. You have no say in how the password is validated. As a result, it protects our accounts from hackers.


Example 2:

Let's say you have a bank account. If you declare your balance variable as a public variable in the bank software, your account balance will be known as public, and anyone can see it.

As a result, they declare the balance variable as private in order to

 keep your account safe, so that no one can see your account

 balance. The person who needs to see his account balance will only

 be able to access private members via methods defined within that class, and this method will ask for

 your account holder name or user Id, as well as your password, for authentication. Thus, using the

 concept of data hiding, we can achieve security. This is known as encapsulation.

2. Abstraction in Java

Another OOPs principle that manages complexity is an abstraction in Java. It is the process of concealing complex internal implementation details from the user and providing only the functionality that is required. It hides all unnecessary data so that users can only work with what is necessary. It filters out all non-essential information and displays only the most important information to users. 

 

Realtime Example of Abstraction  

Example 1:

Let's look at another real-world example. A car owner understands how to operate it. He understands various car components and how to use them. For example, a car owner understands that pressing the accelerator pedal increases the speed of the vehicle while pressing the brake pedal decreases it.

You only need to know how to use these components and not how they work to perform these simple actions.

Example 2:

 When you need to send an SMS from your phone, all you have to do is type the text and hit the send

 button. However, you are unaware of the internal processing of the message delivery.

 3. Inheritance in Java

In Java, inheritance refers to the process of creating a new class by reusing existing class functionality. In other words, inheritance is the process by which a child class inherits all of the parent class's properties and behaviors. [A mechanism by which one class acquires property from another.]

The existing class is known as the parent class (a more general class), and the new class is known as the child class (a more specialized class). The data and behavior of the child class are inherited from the parent class.


Realtime Example of Inheritance 

Example 1:

Parent-Child Relationship, In the real world, a child inherits the characteristics of his or her parents, such as the mother's beauty and the father's intelligence, as shown in the figure below.


class A {
  // statements
}

class A extends Object{
  // statements.
}

Because a car is a type of Vehicle, the inheritance hierarchy may begin with the Vehicle class:





  public class Vehicle{..........}
  public class Car extends Vehicle{..........}
  public class Nissan extends Car{............}

 

4. Polymorphism in Java

    Polymorphism is a fundamental concept in the object-oriented programming language Java (OOPs). The word "poly" means "many," and the word "morphs" means "forms."

As a result, polymorphism means "many forms." That is something that can take many different forms.

Polymorphism is the concept of performing a single task in multiple ways. Polymorphism in Java refers to when a single entity behaves differently in different situations. 

 




 class Shape
{
public void area(){
//statement
}
}

class Square extends Shape{
public void area(){   // overridden method
}
  // other methods or variables decolaration 
}

class Circle extends Shape{
  public void area(){  // overridden method
}
  // other methods or variables declaration
}

class Shapes{
  public static void main(String[] args){
	Shape a = new Square();  // upcasting Square to Shape
        Shape b = new Circle();  // upcasting Circle to Shape
        a.area();     // area a square
        b.area();     // area a circle
}
}


Realtime Example of Polymorphism 

Example :

Human behavior is the best example of polymorphism. One person can exhibit disparate behavior. For example, a person may act as an office employee, a customer in a shopping mall, a bus/train passenger, a student in school, and a son at home. 

 




Comments

Popular posts from this blog

React JWT Authentication with Axios: Handling Access and Refresh Tokens

What is Sales Force Automation (SFA) and Why Your Business Needs It

Seedr: The Ultimate Torrent Downloader