BAEL-5132 Abstract class constructors examples (#11389)

This commit is contained in:
andresluzu 2021-11-18 06:38:36 -05:00 committed by GitHub
parent 52d830bcbe
commit 676f458032
13 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.abstractconstructors.defaultconstructor;
public abstract class AbstractClass {
// compiler creates a default constructor
}

View File

@ -0,0 +1,8 @@
package com.baeldung.abstractconstructors.defaultconstructor;
public class ConcreteClass extends AbstractClass {
public ConcreteClass() {
super();
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.abstractconstructors.noargs;
public abstract class AbstractClass {
public AbstractClass() {
System.out.println("Initializing AbstractClass");
}
}

View File

@ -0,0 +1,4 @@
package com.baeldung.abstractconstructors.noargs;
public class ConcreteClassA extends AbstractClass {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.abstractconstructors.noargs;
public class ConcreteClassB extends AbstractClass {
public ConcreteClassB() {
System.out.println("Initializing ConcreteClassB");
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.abstractconstructors.noargs;
public abstract class Counter {
int value;
private Counter() {
this.value = 0;
System.out.println("Counter No-Arguments constructor");
}
public Counter(int value) {
this.value = value;
System.out.println("Parametrized Counter constructor");
}
abstract int increment();
}

View File

@ -0,0 +1,13 @@
package com.baeldung.abstractconstructors.noargs;
public class SimpleCounter extends Counter {
public SimpleCounter(int value) {
super(value);
}
@Override
int increment() {
return ++value;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.abstractconstructors.parametrized;
public abstract class Car {
private int distance;
private Car(int distance) {
this.distance = distance;
}
public Car() {
this(0);
System.out.println("Car default constructor");
}
abstract String getInformation();
protected void display() {
String info = new StringBuilder(getInformation())
.append("\nDistance: " + getDistance())
.toString();
System.out.println(info);
}
public int getDistance() {
return distance;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.abstractconstructors.parametrized;
public class ElectricCar extends Car {
int chargingTime;
public ElectricCar(int chargingTime) {
this.chargingTime = chargingTime;
}
@Override
String getInformation() {
return new StringBuilder("Electric Car")
.append("\nCharging Time: " + chargingTime)
.toString();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.abstractconstructors.parametrized;
public class FuelCar extends Car {
String fuel;
public FuelCar(String fuel) {
this.fuel = fuel;
}
@Override
String getInformation() {
return new StringBuilder("Fuel Car")
.append("\nFuel type: " + fuel)
.toString();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.abstractconstructors.noargs;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class AbstractClassUnitTest {
@Test
void givenNoArgsAbstractConstructor_whenNewSubclassA_thenCalled() {
assertNotNull(new ConcreteClassA());
}
@Test
void givenNoArgsAbstractConstructor_whenNewSubclassB_thenCalled() {
assertNotNull(new ConcreteClassB());
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.abstractconstructors.noargs;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class CounterUnitTest {
@Test
void givenNoArgAbstractConstructor_whenSubclassCreation_thenCalled() {
Counter counter = new SimpleCounter(1);
assertNotNull(counter);
assertEquals(1, counter.value);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.abstractconstructors.parametrized;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class CarUnitTest {
@Test
void givenParametrizedConstructor_whenConcreteCreation_thenCall() {
ElectricCar electricCar = new ElectricCar(8);
assertNotNull(electricCar);
electricCar.display();
System.out.println();
FuelCar fuelCar = new FuelCar("Gasoline");
assertNotNull(fuelCar);
fuelCar.display();
}
}