Method Overloading and Overriding in Java - BAEL-1507 (#3621)
* Initial Commit * Update Multiplier.java * Refactored MethodOverridingUnitTest class * Update Car.java * Update Vehicle.java * Update MethodOverridingUnitTest.java * Update MethodOverridingUnitTest.java * Fix package names
This commit is contained in:
parent
83bc46ee22
commit
4f6988b134
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.methodoverloadingoverriding.application;
|
||||
|
||||
import com.baeldung.methodoverloadingoverriding.model.Car;
|
||||
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
|
||||
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Multiplier multiplier = new Multiplier();
|
||||
System.out.println(multiplier.multiply(10, 10));
|
||||
System.out.println(multiplier.multiply(10, 10, 10));
|
||||
System.out.println(multiplier.multiply(10, 10.5));
|
||||
System.out.println(multiplier.multiply(10.5, 10.5));
|
||||
|
||||
Vehicle vehicle = new Vehicle();
|
||||
System.out.println(vehicle.accelerate(100));
|
||||
System.out.println(vehicle.run());
|
||||
System.out.println(vehicle.stop());
|
||||
|
||||
Vehicle car = new Car();
|
||||
System.out.println(car.accelerate(80));
|
||||
System.out.println(car.run());
|
||||
System.out.println(car.stop());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.methodoverloadingoverriding.model;
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
@Override
|
||||
public String accelerate(long mph) {
|
||||
return "The car accelerates at : " + mph + " MPH.";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.methodoverloadingoverriding.model;
|
||||
|
||||
public class Vehicle {
|
||||
|
||||
public String accelerate(long mph) {
|
||||
return "The vehicle accelerates at : " + mph + " MPH.";
|
||||
}
|
||||
|
||||
public String stop() {
|
||||
return "The vehicle has stopped.";
|
||||
}
|
||||
|
||||
public String run() {
|
||||
return "The vehicle is running.";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.methodoverloadingoverriding.util;
|
||||
|
||||
public class Multiplier {
|
||||
|
||||
public int multiply(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
public int multiply(int a, int b, int c) {
|
||||
return a * b * c;
|
||||
}
|
||||
|
||||
public double multiply(double a, double b) {
|
||||
return a * b;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.methodoverloadingoverriding.test;
|
||||
|
||||
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class MethodOverloadingUnitTest {
|
||||
|
||||
private static Multiplier multiplier;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpMultiplierInstance() {
|
||||
multiplier = new Multiplier();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() {
|
||||
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.methodoverloadingoverriding.test;
|
||||
|
||||
import com.baeldung.methodoverloadingoverriding.model.Car;
|
||||
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class MethodOverridingUnitTest {
|
||||
|
||||
private static Vehicle vehicle;
|
||||
private static Car car;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpVehicleInstance() {
|
||||
vehicle = new Vehicle();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpCarInstance() {
|
||||
car = new Car();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() {
|
||||
assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledRun_thenOneAssertion() {
|
||||
assertThat(vehicle.run()).isEqualTo("The vehicle is running.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledStop_thenOneAssertion() {
|
||||
assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() {
|
||||
assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledRun_thenOneAssertion() {
|
||||
assertThat(car.run()).isEqualTo("The vehicle is running.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledStop_thenOneAssertion() {
|
||||
assertThat(car.stop()).isEqualTo("The vehicle has stopped.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() {
|
||||
assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledRun_thenEqual() {
|
||||
assertThat(vehicle.run()).isEqualTo(car.run());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledStop_thenEqual() {
|
||||
assertThat(vehicle.stop()).isEqualTo(car.stop());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue