-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulationDemo.java
More file actions
41 lines (33 loc) · 1013 Bytes
/
Copy pathEncapsulationDemo.java
File metadata and controls
41 lines (33 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Program to set a value and retrieve that value.
import java.util.Scanner; //importing an in built package 'java.util'
class Wrap
{
private int n; //Instance variable is generally 'private' so that it cannot be accessed directly outside that class.
Wrap()
{
n=0;
}
//Getter and Setter methods help to implement Encapsulation.They are kept 'public'
public void setValue(int x)
{
System.out.println("Setting Value");
n=x;
}
public int getValue()
{
System.out.println("Getting Value");
return n;
}
}
public class EncapsulationDemo //Class containing main() method should have the same name as that of the file.
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int x=sc.nextInt();
Wrap w=new Wrap(); //Creating an object of class 'Wrap'
w.setValue(50);
System.out.println(w.getValue());
}
}