-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Classes_practice.java
More file actions
164 lines (150 loc) · 6.52 KB
/
Java_Classes_practice.java
File metadata and controls
164 lines (150 loc) · 6.52 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Java_Classes_practice
*
* This class demonstrates various important concepts in Java, such as:
* 1. **Class Definition:** The use of `public class` to define a class.
* 2. **Instance Variables:** Examples include 'name', 'age', 'Friend', 'eatFlesh', and 'height'.
* 3. **Access Modifiers:**
* - `public`: Accessible from anywhere (e.g., `name` and `age`).
* - `private`: Accessible only within the class (e.g., `eatFlesh`).
* - `protected`: Accessible within the same package and subclasses (e.g., `height`).
* - `public static final`: Constant declaration for `MAX_HEIGHT`.
* 4. **Constructors:** Demonstrates both parameterized and default constructors.
* 5. **Methods:**
* - Instance methods (`makeMeLord`, `getName`), which act on objects of the class.
* - Static methods (`makeLord`), which can be called without creating an instance of the class.
* 6. **Encapsulation:** The `eatFlesh` variable is private, with access controlled through `isEatFlesh`.
* 7. **This Keyword Usage:** The example showcases self-referencing through the use of `this` implicitly.
* 8. **Reference Types vs Base Types:**
* - Reference types: `Friend` (instance of `Giant`) and `name` (String object).
* - Base types: `int` (age), `boolean` (eatFlesh), `double` (height).
* 9. **Use of `null`:** `Friend` is initialized to `null` in the default constructor.
* 10. **Static and Instance Context:** The difference between static (`makeLord`) and instance methods (`makeMeLord`).
*/
public class Java_Classes_practice {
/**
* The Giant class demonstrates basic OOP concepts such as instance variables,
* constructors, methods, and access control.
*/
public static class Giant {
// Instance variables
public String name;
public int age;
public Giant Friend;
private boolean eatFlesh = false;
protected double height = 8; // in feet
public static final int MAX_HEIGHT = 12; // maximum height
// Constructors
/**
* Parameterized constructor for creating a Giant with specific properties.
*
* @param nm The name of the Giant.
* @param ag The age of the Giant.
* @param etFl Boolean indicating if the Giant eats flesh.
* @param hg The height of the Giant.
*/
Giant(String nm, int ag, boolean etFl, double hg) {
name = nm;
age = ag;
eatFlesh = etFl;
height = hg;
}
/**
* Default constructor for creating a Giant with default properties.
*/
Giant() {
name = "newTall";
age = 100;
Friend = null;
height = 7;
}
// Methods
/**
* Static method to make a Giant a Lord.
*
* @param G The Giant object to be made a Lord.
*/
public static void makeLord(Giant G) {
G.name = "Lord" + G.getRealName();
G.eatFlesh = true; // Only the Giant class can reference this field
}
/**
* Instance method to make the calling Giant object a Lord.
*/
public void makeMeLord() {
name = "Lord" + getRealName();
eatFlesh = true;
}
/**
* Checks if the Giant eats flesh.
*
* @return True if the Giant eats flesh, false otherwise.
*/
public boolean isEatFlesh() {
return eatFlesh;
}
/**
* Sets the height of the Giant.
*
* @param newHeight The new height to set.
*/
public void setHeight(int newHeight) {
height = newHeight;
}
/**
* Gets the name of the Giant. Overrides the real name with a restricted message.
*
* @return A message stating that the name is forbidden.
*/
public String getName() {
return "It is Forbidden to know my name";
}
/**
* Gets the real name of the Giant.
*
* @return The real name of the Giant.
*/
public String getRealName() {
return "Okay ill tell you, it is " + name;
}
/**
* Renames the Giant.
*
* @param newName The new name to assign to the Giant.
*/
public void renameGiant(String newName) {
name = newName;
}
}
/**
* Main method - entry point of the program.
*/
public static void main(String[] args) {
// Creating instances of the Giant class using different constructors
Giant defaultGiant = new Giant(); // Using the default constructor
Giant paramGiant = new Giant("Goliath", 150, true, 9.5); // Using the parameterized constructor
// Accessing and modifying public instance variables
System.out.println("Default Giant Name: " + defaultGiant.name); // Outputs: newTall
defaultGiant.name = "Gigantor"; // Modifying a public variable
System.out.println("Updated Default Giant Name: " + defaultGiant.name);
// Testing encapsulation: Accessing private variable via method
System.out.println("Does paramGiant eat flesh? " + paramGiant.isEatFlesh()); // Outputs: true
// Static method call: making paramGiant a Lord
Giant.makeLord(paramGiant);
System.out.println("Param Giant's name after makeLord: " + paramGiant.getRealName()); // Outputs: LordGoliath
// Testing instance method: making defaultGiant a Lord
defaultGiant.makeMeLord();
System.out.println("Default Giant's name after makeMeLord: " + defaultGiant.getRealName()); // Outputs: LordGigantor
// Modifying protected variable via method
System.out.println("Height before modification: " + defaultGiant.height); // Outputs: 7
defaultGiant.setHeight(10);
System.out.println("Height after modification: " + defaultGiant.height); // Outputs: 10
// Demonstrating access to a static constant
System.out.println("Maximum height a Giant can have: " + Giant.MAX_HEIGHT); // Outputs: 12
// Creating a reference to another Giant object
paramGiant.Friend = defaultGiant;
System.out.println("paramGiant's friend is: " + paramGiant.Friend.getRealName()); // Outputs: LordGigantor
// Displaying forbidden name message via overridden method
System.out.println("Trying to get paramGiant's name: " + paramGiant.getName()); // Outputs: It is Forbidden to know my name
}
}