Merge pull request #10716 from KarthickSridhar/article_interface_vs_abstract_class
[ BAEL-4935 ] - Article - Interface vs Abstract Class in Java
This commit is contained in:
commit
5c03fa6f4e
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
public Car(String vechicleName) {
|
||||
super(vechicleName);
|
||||
}
|
||||
|
||||
public Car(String vechicleName, String vehicleModel) {
|
||||
super(vechicleName, vehicleModel);
|
||||
}
|
||||
|
||||
public Car(String vechicleName, String vehicleModel, Long makeYear) {
|
||||
super(vechicleName, vehicleModel, makeYear);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void start() {
|
||||
// code implementation details on starting a car.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void stop() {
|
||||
// code implementation details on stopping a car.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drive() {
|
||||
// code implementation details on start driving a car.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void changeGear() {
|
||||
// code implementation details on changing the car gear.
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reverse() {
|
||||
// code implementation details on reverse driving a car.
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ImageSender implements Sender {
|
||||
|
||||
@Override
|
||||
public void send(File fileToBeSent) {
|
||||
// image sending implementation code.
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface Sender {
|
||||
|
||||
void send(File fileToBeSent);
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
public abstract class Vehicle {
|
||||
|
||||
private String vehicleName;
|
||||
private String vehicleModel;
|
||||
private Long makeYear;
|
||||
|
||||
public Vehicle(String vehicleName) {
|
||||
this.vehicleName = vehicleName;
|
||||
}
|
||||
|
||||
public Vehicle(String vehicleName, String vehicleModel) {
|
||||
this(vehicleName);
|
||||
this.vehicleModel = vehicleModel;
|
||||
}
|
||||
|
||||
public Vehicle(String vechicleName, String vehicleModel, Long makeYear) {
|
||||
this(vechicleName, vehicleModel);
|
||||
this.makeYear = makeYear;
|
||||
}
|
||||
|
||||
public String getVehicleName() {
|
||||
return vehicleName;
|
||||
}
|
||||
|
||||
public void setVehicleName(String vehicleName) {
|
||||
this.vehicleName = vehicleName;
|
||||
}
|
||||
|
||||
public String getVehicleModel() {
|
||||
return vehicleModel;
|
||||
}
|
||||
|
||||
public void setVehicleModel(String vehicleModel) {
|
||||
this.vehicleModel = vehicleModel;
|
||||
}
|
||||
|
||||
public Long getMakeYear() {
|
||||
return makeYear;
|
||||
}
|
||||
|
||||
public void setMakeYear(Long makeYear) {
|
||||
this.makeYear = makeYear;
|
||||
}
|
||||
|
||||
protected abstract void start();
|
||||
|
||||
protected abstract void stop();
|
||||
|
||||
protected abstract void drive();
|
||||
|
||||
protected abstract void changeGear();
|
||||
|
||||
protected abstract void reverse();
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class VideoSender implements Sender {
|
||||
|
||||
@Override
|
||||
public void send(File fileToBeSent) {
|
||||
// video sending implementation code
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.interfacevsabstractclass.ImageSender;
|
||||
import com.baeldung.interfacevsabstractclass.Sender;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
class SenderUnitTest {
|
||||
|
||||
public final static String IMAGE_FILE_PATH = "/sample_image_file_path/photo.jpg";
|
||||
|
||||
@Test
|
||||
void givenImageUploaded_whenButtonClicked_thenSendImage() {
|
||||
File imageFile = new File(IMAGE_FILE_PATH);
|
||||
|
||||
Sender sender = new ImageSender();
|
||||
sender.send(imageFile);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.interfacevsabstractclass;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.interfacevsabstractclass.Car;
|
||||
import com.baeldung.interfacevsabstractclass.Vehicle;
|
||||
|
||||
class VehicleUnitTest {
|
||||
|
||||
@Test
|
||||
void givenVehicle_whenNeedToDrive_thenStart() {
|
||||
Vehicle car = new Car("BMW");
|
||||
|
||||
car.start();
|
||||
car.drive();
|
||||
car.changeGear();
|
||||
car.stop();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue