Skip to content

Commit 2cb8d35

Browse files
iaforekpedja4
authored andcommitted
BAEL-1737, BAEL-1667 (eugenp#4251)
* BAEL-1737 - Access Modifiers examples * BAEL-1737 - Corrected constructors * BAEL-1737 - Smaller formatting fixes * BAEL-1737 - Updated comments * BAEL-1667 - Select and Option examples. * BAEL-1667 - Corrected typo - closing select tag - that actually made the code to work. COrrected solution works well too and produces valid HTML
1 parent 445153f commit 2cb8d35

7 files changed

Lines changed: 88 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.accessmodifiers;
2+
3+
public class Public {
4+
public Public() {
5+
SuperPublic.publicMethod(); // Available everywhere.
6+
SuperPublic.protectedMethod(); // Available in the same package or subclass.
7+
SuperPublic.defaultMethod(); // Available in the same package.
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.accessmodifiers;
2+
3+
public class SubClass extends SuperPublic {
4+
public SubClass() {
5+
SuperPublic.publicMethod(); // Available everywhere.
6+
SuperPublic.protectedMethod(); // Available in the same package or subclass.
7+
SuperPublic.defaultMethod(); // Available in the same package.
8+
}
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.accessmodifiers;
2+
3+
//Only public or default access modifiers are permitted
4+
public class SuperPublic {
5+
// Always available from anywhere
6+
static public void publicMethod() {
7+
System.out.println(SuperPublic.class.getName() + " publicMethod()");
8+
}
9+
10+
// Available within the same package
11+
static void defaultMethod() {
12+
System.out.println(SuperPublic.class.getName() + " defaultMethod()");
13+
}
14+
15+
// Available within the same package and subclasses
16+
static protected void protectedMethod() {
17+
System.out.println(SuperPublic.class.getName() + " protectedMethod()");
18+
}
19+
20+
// Available within the same class only
21+
static private void privateMethod() {
22+
System.out.println(SuperPublic.class.getName() + " privateMethod()");
23+
}
24+
25+
// Method in the same class = has access to all members within the same class
26+
private void anotherPrivateMethod() {
27+
privateMethod();
28+
defaultMethod();
29+
protectedMethod();
30+
publicMethod(); // Available in the same class only.
31+
}
32+
}
33+
34+
// Only public or default access modifiers are permitted
35+
class SuperDefault {
36+
public void publicMethod() {
37+
System.out.println(this.getClass().getName() + " publicMethod()");
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.accessmodifiers.another;
2+
3+
import com.baeldung.accessmodifiers.SuperPublic;
4+
5+
public class AnotherPublic {
6+
public AnotherPublic() {
7+
SuperPublic.publicMethod(); // Available everywhere.
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.accessmodifiers.another;
2+
3+
import com.baeldung.accessmodifiers.SuperPublic;
4+
5+
public class AnotherSubClass extends SuperPublic {
6+
public AnotherSubClass() {
7+
SuperPublic.publicMethod(); // Available everywhere.
8+
SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.accessmodifiers.another;
2+
3+
import com.baeldung.accessmodifiers.SuperPublic;
4+
5+
public class AnotherSuperPublic {
6+
public AnotherSuperPublic() {
7+
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
8+
}
9+
}

‎spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ <h1>Add Student</h1>
3232
</tr>
3333
<tr>
3434
<td><label th:text="#{msg.percent}" /></td>
35-
<td><input type="text" th:field="*{percentage}" /></td>
35+
<td><select id="percentage" name="percentage">
36+
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}" th:selected="${i==75}"></option>
37+
</select></td>
3638
</tr>
3739
<tr>
3840
<td><input type="submit" value="Submit" /></td>

0 commit comments

Comments
 (0)