Skip to content

Commit 20158bd

Browse files
committed
BAEL-9384 Flexible constructor bodies in java
1 parent 4730807 commit 20158bd

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-25</artifactId>
7+
8+
<parent>
9+
<groupId>com.baeldung.core-java-modules</groupId>
10+
<artifactId>core-java-modules</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
</parent>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<configuration>
20+
<source>25</source>
21+
<target>25</target>
22+
<compilerArgs>--enable-preview</compilerArgs>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
28+
</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)