diff --git a/core-java-modules/core-java-25/pom.xml b/core-java-modules/core-java-25/pom.xml index 6455972a0cce..645f23f0accd 100644 --- a/core-java-modules/core-java-25/pom.xml +++ b/core-java-modules/core-java-25/pom.xml @@ -24,5 +24,4 @@ - - \ No newline at end of file + diff --git a/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/Coffee.java b/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/Coffee.java new file mode 100644 index 000000000000..c6e5ce3c69b3 --- /dev/null +++ b/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/Coffee.java @@ -0,0 +1,17 @@ +package com.baeldung.flexibleconstructorbodies; + +public class Coffee { + + int water; + int milk; + + public Coffee(int water, int milk) { + this.water = water; + this.milk = milk; + } + + public int getTotalVolume() { + return water + milk; + } + +} diff --git a/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/SmallCoffee.java b/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/SmallCoffee.java new file mode 100644 index 000000000000..5961bf13526b --- /dev/null +++ b/core-java-modules/core-java-25/src/main/java/com/baeldung/flexibleconstructorbodies/SmallCoffee.java @@ -0,0 +1,17 @@ +package com.baeldung.flexibleconstructorbodies; + +public class SmallCoffee extends Coffee { + + String topping; + + public SmallCoffee(int water, int milk, String topping) { + int maxCupVolume = 100; + int totalVolume = water + milk; + if(totalVolume > maxCupVolume) { + throw new IllegalArgumentException(); + } + this.topping = topping; + super(water, milk); + } + +} diff --git a/core-java-modules/core-java-25/src/test/java/FlexibleConstructorBodiesTest.java b/core-java-modules/core-java-25/src/test/java/FlexibleConstructorBodiesTest.java new file mode 100644 index 000000000000..b5005895476e --- /dev/null +++ b/core-java-modules/core-java-25/src/test/java/FlexibleConstructorBodiesTest.java @@ -0,0 +1,15 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.Test; + +import com.baeldung.flexibleconstructorbodies.SmallCoffee; + +public class FlexibleConstructorBodiesTest { + + @Test + public void test() { + SmallCoffee smallCoffee = new SmallCoffee(30,40, "none"); + assertEquals(70, smallCoffee.getTotalVolume()); + } + +}