Skip to content

Commit 50b2ede

Browse files
Merge pull request #18808 from sam-gardner/BAEL-9384-Flexible-constructor-bodies-in-java
BAEL-9384 Flexible constructor bodies in java
2 parents b192461 + 85a37a9 commit 50b2ede

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

core-java-modules/core-java-25/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424
</plugin>
2525
</plugins>
2626
</build>
27-
28-
</project>
27+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.flexibleconstructorbodies;
2+
3+
public class Coffee {
4+
5+
int water;
6+
int milk;
7+
8+
public Coffee(int water, int milk) {
9+
this.water = water;
10+
this.milk = milk;
11+
}
12+
13+
public int getTotalVolume() {
14+
return water + milk;
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.flexibleconstructorbodies;
2+
3+
public class SmallCoffee extends Coffee {
4+
5+
String topping;
6+
7+
public SmallCoffee(int water, int milk, String topping) {
8+
int maxCupVolume = 100;
9+
int totalVolume = water + milk;
10+
if(totalVolume > maxCupVolume) {
11+
throw new IllegalArgumentException();
12+
}
13+
this.topping = topping;
14+
super(water, milk);
15+
}
16+
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.Test;
4+
5+
import com.baeldung.flexibleconstructorbodies.SmallCoffee;
6+
7+
public class FlexibleConstructorBodiesTest {
8+
9+
@Test
10+
public void test() {
11+
SmallCoffee smallCoffee = new SmallCoffee(30,40, "none");
12+
assertEquals(70, smallCoffee.getTotalVolume());
13+
}
14+
15+
}

0 commit comments

Comments
 (0)