Skip to main content

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION 

 This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.

 A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.

  Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will be same till Object destruction and we can only fetch these value but we will be unable to update or modified.

In ECLIPSE or INTELLIJ , we have in-build  option to generate getter and setter method of any CLASS properties by just right clicking and go to generate option and select getter and setter , after that you will be asked to select the properties of CLASS for which you want to generate the getter and setter, just select the properties it can be one or all or some particular it's all depends on our business requirement.

Below is the screenshot to generate getter and setter method of any CLASS's properties.



 For example you can consider Database or MQ Configuration properties , while initialising Object of database or MQ you need set the properties only once after that you don't need to update the properties as this will be common throughout the application so in this case all the properties should be private without setter method as we will initialise all the properties at the time of Object creation and later same should be used so we will keep public getter method only in class.

   There are some other concept which can be little similar to this CONCEPT in JAVA which we will discuss more in details once we will start those topics but for now i will just give overview.

JAVA IS PASS BY VALUE OR PASS BY REFERENCE ?

Answer to this question is JAVA IS PASS BY VALUE and java always pass value to the methods, in case of variable java copies the value and assigned to new variable defined in method block but in case of Object we pass the reference of Object only which has been created via NEW keyword which points to real Object in Heap Memory , that's why any changes to that Object reflects to original Object of heap memory..

That's all about OOPS Concept In Java. Please go through all the below link of OOPS concept in JAVA.


OOPS CONCEPT :
  1. OOPS CONCEPTS IN JAVA
  2. INHERITANCE IN JAVA
  3. ABSTRACTION IN JAVA
  4. POLYMORPHISM IN JAVA
  5. ENCAPSULATION IN JAVA


Comments

Popular posts from this blog

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...