-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation.java
More file actions
129 lines (98 loc) · 3.66 KB
/
Encapsulation.java
File metadata and controls
129 lines (98 loc) · 3.66 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Encapsulation = Encapsulation is all about hiding the internal state of an object and only
allowing controlled access through defined methods.It helps protect data from
unintended interference and keeps your code organized and secure.
It means combining data and its functions/methods in a single entity
or properties and methods are combining in a single unit or capsule.
(Encapsulation is used in Data Hiding) */ //Local Variable = The variable that is declared inside a method
//Instance variable = The variable that declared inside the class but outside the method
//We can asscess objects within same class:
// CASE 1
class Encapsulation {
private String name;
private int age;
public static void main(String[] args) {
Encap obj = new Encap();
obj.name = "Kushant";
obj.age = 21;
System.out.println(obj.name);
System.out.println(obj.age);
}
}
// CASE 2
class Parent {
private String name = "Kushant"; //we cannot access private objects of differet classes
private int age = 21;
public String getname() { //by (get)/Getters we can easily read the values that user makes private
return name;
}
public int getage() {
return age;
}
}
public class Encapsulation {
public static void main(String[] args) {
Parent obj = new Parent();
// obj.name = "Kushant";
// obj.age = 21;
// System.out.println(obj.name);
// System.out.println(obj.age);
System.out.println(obj.getname());
System.out.println(obj.getage());
}
}
// CASE 3
/* Incase if user dont describe values and i want to print my values:
Setters come:- */
class Big {
private String name;
private int age;
public String getname() { //use getters to access private
return name;
}
public int getage() {
return age;
}
public void setname(String n) { //use setters to set values in private things
name = n;
}
public void setage(int a) {
age = a;
}
}
public class Encapsulation {
public static void main(String[] args) {
Big obj = new Big();
obj.setname("Anant"); //to set name & age
obj.setage(21);
System.out.println(obj.getname()); //to print the name and age
System.out.println(obj.getage());
}
}
/* CASE 4
If user set the values in private so we can also change it :-
Setters are used to set values and we can also change the values according to our need */
class Case4 {
private String name = "Kushant";
private int age = 11;
public String getname() { //use getters to access private
return name;
}
public int getage() {
return age;
}
public void setname(String n) { //use setters to set values in private things
name = n;
}
public void setage(int a) {
age = a;
}
}
public class Encapsulation {
public static void main(String[] args) {
Case4 obj = new Case4();
obj.setname("Mukesh"); //to set name & age
obj.setage(45);
System.out.println(obj.getname()); //to print the name and age
System.out.println(obj.getage());
}
}