-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjects.java
More file actions
37 lines (29 loc) · 1013 Bytes
/
Objects.java
File metadata and controls
37 lines (29 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
public class Objects {
// Java objects hold a state, state are variables which are saved together within an object, we call them fields or member variables.
int x;
int y;
// Adding constructors
// 1. Adding constructor for ensuring the class is called with parameters
Objects(int x, int y) {
this.x = x;
this.y = y;
}
// 2. A constructor where the class will not be called with parameters
Objects() {
this(0, 0);
}
// The class can now be called in several ways
// Adding methods
Objects obj() {
return new Objects();
// or
// return new Objects(x, y);
}
protected String s;
}
// Above class can now be called with parameters as follows
//var obj = new Objects(x, y)
// private and public keywords
// private - the class itself can access the variable or method
// public - can be accessed anywhere
// protected - can be accessed by the class itself of the extended classes (through inheritance)