Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class encap{
- public static void main(String Args[])
- {
- encap_demo obj = new encap_demo();
- System.out.println("public variables " + obj.var3 + " " +" "+ obj.var4);
- System.out.println("private variables " + obj.get_var1() + " " +" "+ obj.get_var2());
- obj.set_var1(100);
- obj.set_var2(200);
- obj.var3 = 300;
- obj.var4 = 400;
- System.out.println("\npublic variables after change " + obj.var3 + " " +" "+ obj.var4);
- System.out.println("private variables after change " + obj.get_var1() + " " +" "+ obj.get_var2());
- }
- }
- class encap_demo {
- private int var1, var2;
- public int var3, var4;
- encap_demo()
- {
- System.out.println("Constructor called");
- var1 = 1;
- var2 = 2;
- var3 = 3;
- var4 = 4;
- }
- int get_var1()
- {
- return var1;
- }
- int get_var2()
- {
- return var2;
- }
- void set_var1(int v)
- {
- var1 = v;
- }
- void set_var2(int v)
- {
- var2 = v;
- }
- }
Add Comment
Please, Sign In to add comment