Advanced Java Programming

Image
                          Advanced Java Programming Advanced Java Programming :-   Introduction to advance java   As most of us already know that if we want to make normal applications it  can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please  have a look the video below.                              Learn with Resh u Advanced Java Programming Course Figure - 1.3 I hope you understood by the slide why Advanced Java is essential. For your better understanding, I have divided this article int

Encapsulation in java


      Encapsulation in java




Encapsulation


The meaning of Encapsulation, is to make sure that "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

Get and Set

You learned from the previous blogs that private variables can only be accessed within the same class (an outside class has no access to it). However, it is possible to access them if we provide public get and set methods.


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


Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:


Example

package com.encapsulation.examples;

public class MyEncapsulation {
 
  private String name;
  private String idNum;
  private int age;
   
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }

 public String getIdNum() {
  return idNum;
 }

 public void setIdNum(String idNum) {
  this.idNum = idNum;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }
 
}
public class Test {

 public static void main(String args[]) {
 
  MyEncapsulation me = new MyEncapsulation();
  
  me.setAge(50);
  
  me.setIdNum("AB123");
  
  me.setName("Reshu");
  
  System.out.println("Name : " + me.getName() + " Age : " + me.getAge() + " Id : "+ me.getIdNum());
  
  
 }
 

Example explained

The get method returns the value of the variable name.
The set method takes a parameter (name) and assigns it to the name variable. The this keyword is used to refer to the current object.
However, as the name variable is declared as private, we cannot access it from outside this class:

Example

public class Test{
  public static void main(String[] args) {
     MyEncapsulation me = new MyEncapsulation();

      me.name = "John";  // error
    System.out.println(me.name); // error 
  }
}
If the variable was declared as public, we would expect the following output:

John                                          

but, as we try to access a private variable, we get an error:                                               
Test.java:4: error: name has private access in MyEncapsulation             me.name = "John";                                             
  ^
Test.java:5: error: name has private access in MyEncapsulation   
System.out.println(me.name);                                                                    ^                                           
2 errors    
Instead, we use the getName() and setName() methods to acccess and update the variable:

Example

public class Test{
  public static void main(String[] args) {
   MyEncapsulation me = new MyEncapsulation();

    me.setName("John"); // Set the value of the name variable to "John"
    System.out.println(me.getName());
  }
}

// Outputs "John"

Why Encapsulation?

  • Better control of class attributes and methods
  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  • Flexible: the programmer can change one part of the code without affecting other parts
  • Increased security of data







For Complete Explanation of this topic in hindi Please visit My Youtube Channel 





                  Thanks For Watching!!!


Recommended Posts:

·       Overriding in Java
·       Overloading in Java
·       Interfaces in Java
·       Loops in Java
·       Variables in Java

Advanced Java Programming

         Abstraction in Java
·       What is Java
·       Enabling Remote Debug






Comments

Popular posts from this blog

Advanced Java Programming

Updating version numbers of modules in a multi-module Maven project