[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: ### Relevant Articles:
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors) - [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) - [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
- [Raw Types in Java](https://www.baeldung.com/raw-types-java) - [Raw Types in Java](https://www.baeldung.com/raw-types-java)
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces) - [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts) - [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object) - [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) - [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) - [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) - [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) - [[<-- 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

@ -54,7 +54,6 @@ class BankAccountCopyConstructor extends BankAccount {
this.balance = 0.0f; this.balance = 0.0f;
} }
} }
class BankAccountChainedConstructors extends BankAccount { class BankAccountChainedConstructors extends BankAccount {
public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) { public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) {
this.name = name; this.name = name;

View File

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

View File

@ -9,7 +9,12 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <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> </project>