[JAVA-621] Flattened modules hierarchy
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
## Core Java Lang OOP - Constructors
|
||||
|
||||
This module contains article about constructors in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
|
||||
- [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
|
||||
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
|
||||
28
core-java-modules/core-java-lang-oop-constructors/pom.xml
Normal file
28
core-java-modules/core-java-lang-oop-constructors/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-lang-oop-constructors</artifactId>
|
||||
<name>core-java-lang-oop-constructors</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class BankAccount {
|
||||
String name;
|
||||
LocalDateTime opened;
|
||||
double balance;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LocalDateTime getOpened() {
|
||||
return opened;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return this.balance;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountEmptyConstructor extends BankAccount {
|
||||
public BankAccountEmptyConstructor() {
|
||||
this.name = "";
|
||||
this.opened = LocalDateTime.now();
|
||||
this.balance = 0.0d;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountParameterizedConstructor extends BankAccount {
|
||||
public BankAccountParameterizedConstructor(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountCopyConstructor extends BankAccount {
|
||||
public BankAccountCopyConstructor(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public BankAccountCopyConstructor(BankAccount other) {
|
||||
this.name = other.name;
|
||||
this.opened = LocalDateTime.now();
|
||||
this.balance = 0.0f;
|
||||
}
|
||||
}
|
||||
class BankAccountChainedConstructors extends BankAccount {
|
||||
public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public BankAccountChainedConstructors(String name) {
|
||||
this(name, LocalDateTime.now(), 0.0f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class Transaction {
|
||||
final BankAccountEmptyConstructor bankAccount;
|
||||
final LocalDateTime date;
|
||||
final double amount;
|
||||
|
||||
public Transaction(BankAccountEmptyConstructor account, LocalDateTime date, double amount) {
|
||||
this.bankAccount = account;
|
||||
this.date = date;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compilation Error :'(, all final variables must be explicitly initialised.
|
||||
* public Transaction() {
|
||||
* }
|
||||
*/
|
||||
|
||||
public void invalidMethod() {
|
||||
// this.amount = 102.03; // Results in a compiler error. You cannot change the value of a final variable.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Employee {
|
||||
|
||||
protected int id;
|
||||
protected String name;
|
||||
protected Date startDate;
|
||||
|
||||
public Employee(int id, String name, Date startDate) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Employee(Employee employee) {
|
||||
this.id = employee.id;
|
||||
this.name = employee.name;
|
||||
this.startDate = new Date(employee.startDate.getTime());
|
||||
}
|
||||
|
||||
Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public Employee copy() {
|
||||
return new Employee(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Manager extends Employee {
|
||||
|
||||
private List<Employee> directReports;
|
||||
|
||||
public Manager(int id, String name, Date startDate, List<Employee> directReports) {
|
||||
super(id, name, startDate);
|
||||
this.directReports = directReports;
|
||||
}
|
||||
|
||||
public Manager(Manager manager) {
|
||||
super(manager.id, manager.name, manager.startDate);
|
||||
this.directReports = manager.directReports.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee copy() {
|
||||
return new Manager(this);
|
||||
}
|
||||
|
||||
List<Employee> getDirectReport() {
|
||||
return this.directReports;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClass {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClass() {
|
||||
//uncomment this to see the supertype compiler error:
|
||||
//this(myField1);
|
||||
}
|
||||
|
||||
public MyClass(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution1 {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution1() {
|
||||
myField2 = myField1;
|
||||
}
|
||||
|
||||
public MyClassSolution1(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution2 {
|
||||
|
||||
private int myField1 = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution2() {
|
||||
setupMyFields(myField1);
|
||||
}
|
||||
|
||||
public MyClassSolution2(int i) {
|
||||
setupMyFields(i);
|
||||
}
|
||||
|
||||
private void setupMyFields(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyClassSolution3 {
|
||||
|
||||
private static final int SOME_CONSTANT = 10;
|
||||
private int myField2;
|
||||
|
||||
public MyClassSolution3() {
|
||||
this(SOME_CONSTANT);
|
||||
}
|
||||
|
||||
public MyClassSolution3(int i) {
|
||||
myField2 = i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.supertypecompilerexception;
|
||||
|
||||
public class MyException extends RuntimeException {
|
||||
private int errorCode = 0;
|
||||
|
||||
public MyException(String message) {
|
||||
//uncomment this to see the supertype compiler error:
|
||||
//super(message + getErrorCode());
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Month;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ConstructorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNoExplicitContructor_whenUsed_thenFails() {
|
||||
BankAccount account = new BankAccount();
|
||||
Assertions.assertThatThrownBy(() -> {
|
||||
account.toString();
|
||||
}).isInstanceOf(Exception.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoArgumentConstructor_whenUsed_thenSucceeds() {
|
||||
BankAccountEmptyConstructor account = new BankAccountEmptyConstructor();
|
||||
Assertions.assertThatCode(() -> {
|
||||
account.toString();
|
||||
}).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParameterisedConstructor_whenUsed_thenSucceeds() {
|
||||
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
|
||||
BankAccountParameterizedConstructor account =
|
||||
new BankAccountParameterizedConstructor("Tom", opened, 1000.0f);
|
||||
|
||||
Assertions.assertThatCode(() -> {
|
||||
account.toString();
|
||||
}).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyContructor_whenUser_thenMaintainsLogic() {
|
||||
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
|
||||
BankAccountCopyConstructor account = new BankAccountCopyConstructor("Tim", opened, 1000.0f);
|
||||
BankAccountCopyConstructor newAccount = new BankAccountCopyConstructor(account);
|
||||
|
||||
assertThat(account.getName()).isEqualTo(newAccount.getName());
|
||||
assertThat(account.getOpened()).isNotEqualTo(newAccount.getOpened());
|
||||
|
||||
assertThat(newAccount.getBalance()).isEqualTo(0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChainedConstructor_whenUsed_thenMaintainsLogic() {
|
||||
BankAccountChainedConstructors account = new BankAccountChainedConstructors("Tim");
|
||||
BankAccountChainedConstructors newAccount = new BankAccountChainedConstructors("Tim", LocalDateTime.now(), 0.0f);
|
||||
|
||||
assertThat(account.getName()).isEqualTo(newAccount.getName());
|
||||
assertThat(account.getBalance()).isEqualTo(newAccount.getBalance());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmployeeUnitTest {
|
||||
@Test
|
||||
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||
Date d1 = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||
Employee e2 = new Employee(e1);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertEquals(d1, e2.getStartDate());
|
||||
|
||||
d1.setTime(456);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertNotEquals(d1, e2.getStartDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||
Date d1 = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||
Employee e2 = e1.copy();
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertEquals(d1, e2.getStartDate());
|
||||
|
||||
d1.setTime(456);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertNotEquals(d1, e2.getStartDate());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ManagerUnitTest {
|
||||
@Test
|
||||
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||
Date startDate = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||
Employee e2 = new Employee(e1);
|
||||
List<Employee> directReports = new ArrayList<Employee>();
|
||||
directReports.add(e1);
|
||||
directReports.add(e2);
|
||||
|
||||
Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||
Manager m2 = new Manager(m1);
|
||||
List<Employee> directReports1 = m1.getDirectReport();
|
||||
List<Employee> directReports2 = m2.getDirectReport();
|
||||
assertEquals(directReports1.size(), directReports2.size());
|
||||
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||
|
||||
// clear m1's direct reports list. m2's list should not be affected
|
||||
directReports.clear();
|
||||
directReports1 = m1.getDirectReport();
|
||||
directReports2 = m2.getDirectReport();
|
||||
assertEquals(0, directReports1.size());
|
||||
assertEquals(2, directReports2.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||
Date startDate = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||
Employee e2 = new Employee(e1);
|
||||
List<Employee> directReports = new ArrayList<Employee>();
|
||||
directReports.add(e1);
|
||||
directReports.add(e2);
|
||||
|
||||
// a Manager object whose declaration type is Employee.
|
||||
Employee source = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||
Employee clone = source.copy();
|
||||
|
||||
// after copy, clone should be still a Manager object.
|
||||
assertTrue(clone instanceof Manager);
|
||||
List<Employee> directReports1 = ((Manager) source).getDirectReport();
|
||||
List<Employee> directReports2 = ((Manager) clone).getDirectReport();
|
||||
assertEquals(directReports1.size(), directReports2.size());
|
||||
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||
|
||||
// clear source's direct reports list. clone's list should not be affected
|
||||
directReports.clear();
|
||||
directReports1 = ((Manager) source).getDirectReport();
|
||||
directReports2 = ((Manager) clone).getDirectReport();
|
||||
assertEquals(0, directReports1.size());
|
||||
assertEquals(2, directReports2.size());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user