From 2cb8d357f43138db03a2721ae423ca784f9915c5 Mon Sep 17 00:00:00 2001 From: iaforek Date: Fri, 15 Jun 2018 08:03:09 +0200 Subject: [PATCH] BAEL-1737, BAEL-1667 (#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 --- .../com/baeldung/accessmodifiers/Public.java | 9 +++++ .../baeldung/accessmodifiers/SubClass.java | 9 +++++ .../baeldung/accessmodifiers/SuperPublic.java | 39 +++++++++++++++++++ .../another/AnotherPublic.java | 9 +++++ .../another/AnotherSubClass.java | 10 +++++ .../another/AnotherSuperPublic.java | 9 +++++ .../main/webapp/WEB-INF/views/addStudent.html | 4 +- 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/Public.java create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/SubClass.java create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java create mode 100644 core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/Public.java b/core-java/src/main/java/com/baeldung/accessmodifiers/Public.java new file mode 100644 index 0000000000..9a64c1d4c4 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/Public.java @@ -0,0 +1,9 @@ +package com.baeldung.accessmodifiers; + +public class Public { + public Public() { + SuperPublic.publicMethod(); // Available everywhere. + SuperPublic.protectedMethod(); // Available in the same package or subclass. + SuperPublic.defaultMethod(); // Available in the same package. + } +} diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/SubClass.java b/core-java/src/main/java/com/baeldung/accessmodifiers/SubClass.java new file mode 100644 index 0000000000..545f48c9ca --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/SubClass.java @@ -0,0 +1,9 @@ +package com.baeldung.accessmodifiers; + +public class SubClass extends SuperPublic { + public SubClass() { + SuperPublic.publicMethod(); // Available everywhere. + SuperPublic.protectedMethod(); // Available in the same package or subclass. + SuperPublic.defaultMethod(); // Available in the same package. + } +} diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java b/core-java/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java new file mode 100644 index 0000000000..7c9554f2c7 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java @@ -0,0 +1,39 @@ +package com.baeldung.accessmodifiers; + +//Only public or default access modifiers are permitted +public class SuperPublic { + // Always available from anywhere + static public void publicMethod() { + System.out.println(SuperPublic.class.getName() + " publicMethod()"); + } + + // Available within the same package + static void defaultMethod() { + System.out.println(SuperPublic.class.getName() + " defaultMethod()"); + } + + // Available within the same package and subclasses + static protected void protectedMethod() { + System.out.println(SuperPublic.class.getName() + " protectedMethod()"); + } + + // Available within the same class only + static private void privateMethod() { + System.out.println(SuperPublic.class.getName() + " privateMethod()"); + } + + // Method in the same class = has access to all members within the same class + private void anotherPrivateMethod() { + privateMethod(); + defaultMethod(); + protectedMethod(); + publicMethod(); // Available in the same class only. + } +} + +// Only public or default access modifiers are permitted +class SuperDefault { + public void publicMethod() { + System.out.println(this.getClass().getName() + " publicMethod()"); + } +} diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java new file mode 100644 index 0000000000..32eea36854 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java @@ -0,0 +1,9 @@ +package com.baeldung.accessmodifiers.another; + +import com.baeldung.accessmodifiers.SuperPublic; + +public class AnotherPublic { + public AnotherPublic() { + SuperPublic.publicMethod(); // Available everywhere. + } +} diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java new file mode 100644 index 0000000000..3d9dc3f119 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java @@ -0,0 +1,10 @@ +package com.baeldung.accessmodifiers.another; + +import com.baeldung.accessmodifiers.SuperPublic; + +public class AnotherSubClass extends SuperPublic { + public AnotherSubClass() { + SuperPublic.publicMethod(); // Available everywhere. + SuperPublic.protectedMethod(); // Available in subclass. Let's note different package. + } +} diff --git a/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java new file mode 100644 index 0000000000..3932e6f990 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java @@ -0,0 +1,9 @@ +package com.baeldung.accessmodifiers.another; + +import com.baeldung.accessmodifiers.SuperPublic; + +public class AnotherSuperPublic { + public AnotherSuperPublic() { + SuperPublic.publicMethod(); // Available everywhere. Let's note different package. + } +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html index f3d28d7ef4..34a6dec4da 100644 --- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html @@ -32,7 +32,9 @@