BAEL-5132 Abstract class constructors examples (#11389)
This commit is contained in:
parent
52d830bcbe
commit
676f458032
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.abstractconstructors.defaultconstructor;
|
||||
|
||||
public abstract class AbstractClass {
|
||||
// compiler creates a default constructor
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.abstractconstructors.defaultconstructor;
|
||||
|
||||
public class ConcreteClass extends AbstractClass {
|
||||
|
||||
public ConcreteClass() {
|
||||
super();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.abstractconstructors.noargs;
|
||||
|
||||
public abstract class AbstractClass {
|
||||
|
||||
public AbstractClass() {
|
||||
System.out.println("Initializing AbstractClass");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.abstractconstructors.noargs;
|
||||
|
||||
public class ConcreteClassA extends AbstractClass {
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.abstractconstructors.noargs;
|
||||
|
||||
public class ConcreteClassB extends AbstractClass {
|
||||
|
||||
public ConcreteClassB() {
|
||||
System.out.println("Initializing ConcreteClassB");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue