Skip to content

Commit 4f6988b

Browse files
Alejandro GervasioKevinGilmore
authored andcommitted
Method Overloading and Overriding in Java - BAEL-1507 (eugenp#3621)
* Initial Commit * Update Multiplier.java * Refactored MethodOverridingUnitTest class * Update Car.java * Update Vehicle.java * Update MethodOverridingUnitTest.java * Update MethodOverridingUnitTest.java * Fix package names
1 parent 83bc46e commit 4f6988b

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.methodoverloadingoverriding.application;
2+
3+
import com.baeldung.methodoverloadingoverriding.model.Car;
4+
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
5+
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
6+
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
Multiplier multiplier = new Multiplier();
11+
System.out.println(multiplier.multiply(10, 10));
12+
System.out.println(multiplier.multiply(10, 10, 10));
13+
System.out.println(multiplier.multiply(10, 10.5));
14+
System.out.println(multiplier.multiply(10.5, 10.5));
15+
16+
Vehicle vehicle = new Vehicle();
17+
System.out.println(vehicle.accelerate(100));
18+
System.out.println(vehicle.run());
19+
System.out.println(vehicle.stop());
20+
21+
Vehicle car = new Car();
22+
System.out.println(car.accelerate(80));
23+
System.out.println(car.run());
24+
System.out.println(car.stop());
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.methodoverloadingoverriding.model;
2+
3+
public class Car extends Vehicle {
4+
5+
@Override
6+
public String accelerate(long mph) {
7+
return "The car accelerates at : " + mph + " MPH.";
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.methodoverloadingoverriding.model;
2+
3+
public class Vehicle {
4+
5+
public String accelerate(long mph) {
6+
return "The vehicle accelerates at : " + mph + " MPH.";
7+
}
8+
9+
public String stop() {
10+
return "The vehicle has stopped.";
11+
}
12+
13+
public String run() {
14+
return "The vehicle is running.";
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.methodoverloadingoverriding.util;
2+
3+
public class Multiplier {
4+
5+
public int multiply(int a, int b) {
6+
return a * b;
7+
}
8+
9+
public int multiply(int a, int b, int c) {
10+
return a * b * c;
11+
}
12+
13+
public double multiply(double a, double b) {
14+
return a * b;
15+
}
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.methodoverloadingoverriding.test;
2+
3+
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import static org.assertj.core.api.Assertions.*;
7+
8+
public class MethodOverloadingUnitTest {
9+
10+
private static Multiplier multiplier;
11+
12+
@BeforeClass
13+
public static void setUpMultiplierInstance() {
14+
multiplier = new Multiplier();
15+
}
16+
17+
@Test
18+
public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() {
19+
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
20+
}
21+
22+
@Test
23+
public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() {
24+
assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000);
25+
}
26+
27+
@Test
28+
public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() {
29+
assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0);
30+
}
31+
32+
@Test
33+
public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() {
34+
assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25);
35+
}
36+
37+
@Test
38+
public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() {
39+
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
40+
}
41+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.methodoverloadingoverriding.test;
2+
3+
import com.baeldung.methodoverloadingoverriding.model.Car;
4+
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import static org.assertj.core.api.Assertions.*;
8+
9+
public class MethodOverridingUnitTest {
10+
11+
private static Vehicle vehicle;
12+
private static Car car;
13+
14+
@BeforeClass
15+
public static void setUpVehicleInstance() {
16+
vehicle = new Vehicle();
17+
}
18+
19+
@BeforeClass
20+
public static void setUpCarInstance() {
21+
car = new Car();
22+
}
23+
24+
@Test
25+
public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() {
26+
assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH.");
27+
}
28+
29+
@Test
30+
public void givenVehicleInstance_whenCalledRun_thenOneAssertion() {
31+
assertThat(vehicle.run()).isEqualTo("The vehicle is running.");
32+
}
33+
34+
@Test
35+
public void givenVehicleInstance_whenCalledStop_thenOneAssertion() {
36+
assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped.");
37+
}
38+
39+
@Test
40+
public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() {
41+
assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH.");
42+
}
43+
44+
@Test
45+
public void givenCarInstance_whenCalledRun_thenOneAssertion() {
46+
assertThat(car.run()).isEqualTo("The vehicle is running.");
47+
}
48+
49+
@Test
50+
public void givenCarInstance_whenCalledStop_thenOneAssertion() {
51+
assertThat(car.stop()).isEqualTo("The vehicle has stopped.");
52+
}
53+
54+
@Test
55+
public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() {
56+
assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100));
57+
}
58+
59+
@Test
60+
public void givenVehicleCarInstances_whenCalledRun_thenEqual() {
61+
assertThat(vehicle.run()).isEqualTo(car.run());
62+
}
63+
64+
@Test
65+
public void givenVehicleCarInstances_whenCalledStop_thenEqual() {
66+
assertThat(vehicle.stop()).isEqualTo(car.stop());
67+
}
68+
}

0 commit comments

Comments
 (0)