[JAVA-621] core-java-lang-oop-modifiers module

* Creation

* Moved code from https://www.baeldung.com/java-static

* Moved code from https://www.baeldung.com/java-final

* Moved code from https://www.baeldung.com/java-public-keyword

* Moved code from https://www.baeldung.com/java-access-modifiers

* Moved code from https://www.baeldung.com/java-private-keyword

* Moved code from https://www.baeldung.com/java-static-default-methods

* Moved code from https://www.baeldung.com/java-strictfp

* Moved article references to the new README.md
This commit is contained in:
dupirefr
2020-04-04 09:42:08 +02:00
parent 6baffe791f
commit c182eaca8b
40 changed files with 52 additions and 12 deletions

View File

@@ -4,10 +4,8 @@ This module contains articles about Object-oriented programming (OOP) in Java
### Relevant Articles:
- [Pass-By-Value as a Parameter Passing Mechanism in Java](https://www.baeldung.com/java-pass-by-value-or-pass-by-reference)
- [Access Modifiers in Java](https://www.baeldung.com/java-access-modifiers)
- [Guide to the super Java Keyword](https://www.baeldung.com/java-super)
- [Guide to the this Java Keyword](https://www.baeldung.com/java-this)
- [Java public Access Modifier](https://www.baeldung.com/java-public-keyword)
- [Nested Classes in Java](https://www.baeldung.com/java-nested-classes)
- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces)
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)

View File

@@ -1,9 +0,0 @@
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.
}
}

View File

@@ -1,9 +0,0 @@
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.
}
}

View File

@@ -1,39 +0,0 @@
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()");
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.accessmodifiers.another;
import com.baeldung.accessmodifiers.SuperPublic;
public class AnotherPublic {
public AnotherPublic() {
SuperPublic.publicMethod(); // Available everywhere.
}
}

View File

@@ -1,10 +0,0 @@
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.
}
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.accessmodifiers.another;
import com.baeldung.accessmodifiers.SuperPublic;
public class AnotherSuperPublic {
public AnotherSuperPublic() {
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
}
}

View File

@@ -1,31 +0,0 @@
package com.baeldung.accessmodifiers.publicmodifier;
import java.util.AbstractList;
import java.util.Arrays;
public class ListOfThree<E> extends AbstractList<E> {
private static final int LENGTH = 3;
private Object[] elements;
public ListOfThree(E[] data) {
if(data == null
|| data.length != LENGTH)
throw new IllegalArgumentException();
this.elements = Arrays.copyOf(data, data.length); //shallow copy
}
@Override
@SuppressWarnings("unchecked")
public E get(int index) {
return (E)elements[index];
}
@Override
public int size() {
return LENGTH;
}
}

View File

@@ -1,7 +0,0 @@
package com.baeldung.accessmodifiers.publicmodifier;
public class SpecialCharacters {
public static final String SLASH = "/";
}

View File

@@ -1,67 +0,0 @@
package com.baeldung.accessmodifiers.publicmodifier;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
public class Student {
private StudentGrade grade; //new data representation
// private int grade; //old data representation
private String name;
private int age;
public void setGrade(int grade) {
this.grade = new StudentGrade(grade);
}
public int getGrade() {
return this.grade.getGrade().intValue(); //int is returned for backward compatibility
}
public Connection getConnection() throws SQLException {
final String URL = "jdbc:h2:~/test";
return DriverManager.getConnection(URL, "sa", "");
}
public void setAge(int age) {
if (age < 0 || age > 150) {
throw new IllegalArgumentException();
}
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return this.name;
}
private class StudentGrade {
private BigDecimal grade = BigDecimal.ZERO;
private Date updatedAt;
public StudentGrade(int grade) {
this.grade = new BigDecimal(grade);
this.updatedAt = new Date();
}
public BigDecimal getGrade() {
return grade;
}
public Date getDate() {
return updatedAt;
}
}
}

View File

@@ -1,94 +0,0 @@
package com.baeldung.accessmodifiers;
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
import com.baeldung.accessmodifiers.publicmodifier.Student;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(Lifecycle.PER_CLASS)
public class PublicAccessModifierUnitTest {
@Test
public void whenUsingBigDecimalIntValueMethod_correspondingIntIsReturned() {
assertEquals(0, new BigDecimal(0).intValue()); //instance member
}
@Test
public void whenUsingIntegerMaxValueField_maxPossibleIntValueIsReturned() {
assertEquals(2147483647, Integer.MAX_VALUE); //static field
}
@Test
public void whenChangingStudentInternalRepresentation_clientCodeWillNotBreak() {
Student student = new Student();
student.setGrade(100);
assertEquals(100, student.getGrade());
}
@Test
public void whenUsingEntrySet_keyValuePairsAreReturned() {
Map<String, String> mapObject = new HashMap<String, String>();
mapObject.put("name", "Alex");
for(Map.Entry<String, String> entry : mapObject.entrySet()) {
assertEquals("name", entry.getKey());
assertEquals("Alex", entry.getValue());
}
}
@Test
public void whenUsingStringToLowerCase_stringTurnsToLowerCase() {
assertEquals("alex", "ALEX".toLowerCase());
}
@Test
public void whenParsingStringOne_parseIntReturns1() {
assertEquals(1, Integer.parseInt("1"));
}
@Test
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
final String url = "jdbc:h2:~/test";
Connection conn = DriverManager.getConnection(url, "sa", "");
assertNotNull(conn);
}
@Test
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
String[] dataSet1 = new String[] {"zero", "one", "two"};
List<String> list1 = new ListOfThree<String>(dataSet1);
//our implemented methods
assertEquals("one", list1.get(1));
assertEquals(3, list1.size());
//inherited implementations
assertEquals(1, list1.indexOf("one"));
String[] dataSet2 = new String[] {"two", "zero", "one"};
List<String> list2 = new ListOfThree<String>(dataSet2);
assertTrue(list1.containsAll(list2));
}
}