Interface vs Abstract Class in Java
This commit is contained in:
parent
45764829f7
commit
f107a2d2fc
|
@ -7,3 +7,4 @@ This module contains articles about Object-oriented programming (OOP) patterns i
|
|||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](https://www.baeldung.com/java-inheritance-composition)
|
||||
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object)
|
||||
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
|
||||
- [Interface vs Abstract Class in Java](https://www.baeldung.com/interface-vs-abstract-class-in-java/)
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.interface_vs_abstract_class;
|
||||
|
||||
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.interface_vs_abstract_class;
|
||||
|
||||
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.interface_vs_abstract_class;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface Sender {
|
||||
|
||||
void send(File fileToBeSent);
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.interface_vs_abstract_class;
|
||||
|
||||
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,13 @@
|
|||
package com.baeldung.interface_vs_abstract_class;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class VideoSender implements Sender {
|
||||
|
||||
@Override
|
||||
public void send(File fileToBeSent) {
|
||||
// video sending implementation code
|
||||
System.out.println("Sending Video...");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.interface_vs_abstract_class;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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,21 @@
|
|||
package com.baeldung.interface_vs_abstract_class;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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