-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectClass.java
74 lines (63 loc) · 2.09 KB
/
ObjectClass.java
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
class Laptop {
String model;
int price;
String serial;
public String toString() {
// return "Hi";//
return model + " " + price;
}
// // do not do this,let us use or id use source action select what you want to
// compare NOTE : I used vs code and installed some exensions
// @Override -- annotation we learn that later
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result + price;
return result;
}
//
// @Override annotions- discuss them later in code
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Laptop other = (Laptop) obj;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (price != other.price)
return false;
return true;
}
// to this generate by ide
// public boolean equals(Laptop that) {
// // if (this.model.equals(this.model) && this.price == that.price) {
// // return true;
// // } else {
// // return false;
// // }
// return (this.model.equals(this.model) && this.price == that.price);
// }
}
public class ObjectClass {
// every class in java extends object class ,so what is there in object class
public static void main(String args[]) {
Laptop obj1 = new Laptop();
obj1.model = "Lenova";
obj1.price = 100;
// System.out.println(obj1);// will print something like this // Laptop@80098//
// here automaticall .toString
// is getting invoked
Laptop obj2 = new Laptop();
obj2.model = "Lenova";
obj2.price = 100;
boolean result = obj1.equals(obj2);
System.out.println(result);
}
}