Showing posts with label Encapsulation. Show all posts
Showing posts with label Encapsulation. Show all posts

Jul 5, 2020

Encapsulation in JAVA | Java Language | Coding Winds

ENCAPSULATION

Hello guys, today we are gonna talk about encapsulation, which is another pillar of OOps in java. So what is encapsulation? Consider a capsule, a capsule consists of powdered medicines which is enclosed in an outer body ,and the pateint who eats the capsule doesn't know that what powdered medicines are actually inside the capsule. So encapsulation is sort off same thing, we write our code but we encapsulate or hide our certain data and so that everyone can't access every data that is inside our code. So the key point of encapsulation is data privacy but apart from that it also allows us to set a certain criteria for taking the parameters from the user.

First let us see it's implementation inside the code

Ex:

package solution;

public class Student {

     private int marks;

     private String name;

     public int getMarks() {

         return marks;

     }

     public void setMarks(int age) {

         this.marks = marks;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         this.name = name;

     }

     }

The first step is to create a separate class file inside the same package where your class with main method exists. Make sure your each variable inside this class should be public, and then finally we can create a getter and setter method (shown below in the example) for each of our parameters inside the class (make sure they are public too).

Note that, this keyword is a reference variable use to reference the current object.

Now we can create an object out of this class inside our main method and call them respectively. The code (that has to be in the main method) is given below

package solution;

public class Coding_Winds {

    

     public static void main(String[] args) {

         Student obj = new Student();

         obj.setMarks(18);

         obj.setName("Mr. Moolchand");

              }

}

Now if we can print out the inputs that we passed

package solution;

public class Coding_Winds {

     public static void main(String[] args) {

         Student obj = new Student();

         obj.setMarks(18);

         obj.setName("Mr. Moolchand");

         System.out.println("The name of the student is "+obj.getName()+" and his/her score is "+obj.getMarks()+"/20");

         }

}

//Output: The name of the student is Mr. Moolchand and his/her score is 18/20

Now this was one thing, what if you want your parameters to be filtered out through a certain criteria? Yes, encapsulation allows us to set the criteria for parameters inside our setter method. Look at the code below

package solution;

public class Student {

     private int marks;

     private String name;

     public int getMarks() {

         return marks;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         if(name.length()>=20) {

              System.out.println("Your nick name please!");

         }

         else {

              System.out.println("The name of the student :"+name);

         }

         this.name = name;

     }

     public void setMarks(int marks) {

         if(marks>20) {

              System.out.print("Invalid Score!");

         }

        else if(marks>17) {

              System.out.print("Grade: A grade");

         }

         else if(marks>14) {

              System.out.print("Grade: B grade");

         }

         else if(marks >11) {

              System.out.print("Grade: C grade");

         }

         else if(marks > 8) {

              System.out.print("Grade: D grade");

         }

         else {

              System.out.print("Failed");

         }

         this.marks = marks;

     }

     }

Now if the user will enter the name that has more than 20 characters or a score that is more than 20, the output will come as shown below the code

package solution;

public class Coding_Winds {

    

     public static void main(String[] args) {

         Student obj = new Student();

         obj.setName("Mr. Bala subramanium Ji");

         obj.setMarks(22);

     }

}

/*Your nick name please!

Invalid Score!

*/

 

Similarly if the user will make sure of the parameters criteria, the output will come accordingly. For example

package solution;

public class Coding_Winds {

    

     public static void main(String[] args) {

         Student obj = new Student();

         obj.setName("Jessica Davis");

         obj.setMarks(15);

     }

}

/*The name of the student :Jessica Davis

Grade: B grade

*/

So this was all about encapsulation, we will keep catching up with you along with more hot blogs. Till then keep reading, and keep coding.


If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!