AnindyaBiswas

encap

May 18th, 2022 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class encap{
  3.     public static void main(String Args[])
  4.     {
  5.         encap_demo obj = new encap_demo();
  6.         System.out.println("public variables " + obj.var3 + " " +" "+ obj.var4);
  7.         System.out.println("private variables " + obj.get_var1() + " " +" "+ obj.get_var2());
  8.         obj.set_var1(100);
  9.         obj.set_var2(200);
  10.         obj.var3 = 300;
  11.         obj.var4 = 400;
  12.         System.out.println("\npublic variables after change " + obj.var3 + " " +" "+ obj.var4);
  13.         System.out.println("private variables after change " + obj.get_var1() + " " +" "+ obj.get_var2());
  14.     }
  15. }
  16.  
  17. class encap_demo {
  18.     private int var1, var2;
  19.     public int var3, var4;
  20.  
  21.     encap_demo()
  22.     {
  23.         System.out.println("Constructor called");
  24.         var1 = 1;
  25.         var2 = 2;
  26.         var3 = 3;
  27.         var4 = 4;
  28.     }
  29.  
  30.     int get_var1()
  31.     {
  32.         return var1;
  33.     }
  34.    
  35.     int get_var2()
  36.     {
  37.         return var2;
  38.     }
  39.    
  40.     void set_var1(int v)
  41.     {
  42.         var1 = v;
  43.     }
  44.    
  45.     void set_var2(int v)
  46.     {
  47.         var2 = v;
  48.     }
  49.    
  50. }
Add Comment
Please, Sign In to add comment