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

* Creation

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

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

* Moved code from https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error

* Moved article references to the new README.md
This commit is contained in:
dupirefr 2020-04-02 21:04:24 +02:00
parent e25ce92d5e
commit 117cb9ce46
16 changed files with 52 additions and 16 deletions

View File

@ -4,15 +4,12 @@ This module contains articles about Object-oriented programming (OOP) in Java
### Relevant Articles:
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors)
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object)
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](https://www.baeldung.com/java-inheritance-composition)
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods)
- [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class)
- [[<-- Prev]](/core-java-modules/core-java-lang-oop)[[More -->]](/core-java-modules/core-java-lang-oop-3)

View File

@ -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)

View 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-lang-oop-modules</artifactId>
<groupId>com.baeldung.core-java-lang-oop-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>

View File

@ -6,7 +6,7 @@ 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);
@ -47,14 +47,13 @@ class BankAccountCopyConstructor extends BankAccount {
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;
@ -65,4 +64,4 @@ class BankAccountChainedConstructors extends BankAccount {
public BankAccountChainedConstructors(String name) {
this(name, LocalDateTime.now(), 0.0f);
}
}
}

View File

@ -1,20 +1,19 @@
package com.baeldung.constructors;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.logging.Logger;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
public class ConstructorUnitTest {
final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName());
@Test
public void givenNoExplicitContructor_whenUsed_thenFails() {
BankAccount account = new BankAccount();
assertThatThrownBy(() -> {
Assertions.assertThatThrownBy(() -> {
account.toString();
}).isInstanceOf(Exception.class);
}
@ -22,7 +21,7 @@ public class ConstructorUnitTest {
@Test
public void givenNoArgumentConstructor_whenUsed_thenSucceeds() {
BankAccountEmptyConstructor account = new BankAccountEmptyConstructor();
assertThatCode(() -> {
Assertions.assertThatCode(() -> {
account.toString();
}).doesNotThrowAnyException();
}
@ -33,7 +32,7 @@ public class ConstructorUnitTest {
BankAccountParameterizedConstructor account =
new BankAccountParameterizedConstructor("Tom", opened, 1000.0f);
assertThatCode(() -> {
Assertions.assertThatCode(() -> {
account.toString();
}).doesNotThrowAnyException();
}

View File

@ -9,7 +9,12 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-oop-modules</artifactId>
<groupId>com.baeldung.core-java-lang-oop-modules</groupId>
<artifactId>core-java-lang-oop-modules</artifactId>
<name>core-java-lang-oop-modules</name>
<packaging>pom</packaging>
<modules>
<module>core-java-lang-oop-constructors</module>
</modules>
</project>