Java Interfaces Examples (#5992)

* Implementing Hexagonal Architecture in java

* Implementing multi-inheritance and Polymorphism using interfaces

* Removing hexagonal code

* Deleting hexagonal architecture code

* fix for unit test names

* Java Interfaces examples
This commit is contained in:
kyleandari 2018-12-25 14:58:14 -05:00 committed by maibin
parent 8fbed2e741
commit 21bde94745
14 changed files with 142 additions and 1 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.interfaces;
public interface Electronic {
//Constant variable
public static final String LED = "LED";
//Abstract method
public int getElectricityUse();
// Static method
public static boolean isEnergyEfficient(String electtronicType) {
if (electtronicType.equals(LED)) {
return true;
}
return false;
}
//Default method
public default void printDescription() {
System.out.println("Electronic Description");
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.interfaces;
public class Employee {
private double salary;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.interfaces;
import java.util.Comparator;
public class EmployeeSalaryComparator implements Comparator<Employee> {
@Override
public int compare(Employee employeeA, Employee employeeB) {
if(employeeA.getSalary() < employeeB.getSalary()){
return -1;
}else if(employeeA.getSalary() > employeeB.getSalary()){
return 1;
}else{
return 0;
}
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.interfaces;
public interface HasColor {
public String getColor();
}

View File

@ -0,0 +1,10 @@
package com.baeldung.interfaces;
import com.baeldung.interfaces.multiinheritance.Transform;
public class Motorcycle implements Transform {
@Override
public void transform() {
// Implementation
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.interfaces;
public class Truck extends Vehicle {
@Override
public void transform() {
// implementation
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.interfaces;
import com.baeldung.interfaces.multiinheritance.Transform;
public abstract class Vehicle implements Transform {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.interfaces.multiinheritance;
public class Car implements Fly, Transform {
@Override
public void fly() {
System.out.println("I can Fly!!");
}
@Override
public void transform() {
System.out.println("I can Transform!!");
}
}

View File

@ -1,5 +1,10 @@
package com.baeldung.interfaces.multiinheritance;
public interface Transform {
void transform();
default void printSpecs(){
System.out.println("Transform Specification");
}
}

View File

@ -18,4 +18,8 @@ public class Circle implements Shape {
return Math.PI * (radius * radius);
}
@Override
public String getColor() {
return "green";
}
}

View File

@ -6,6 +6,10 @@ public class DisplayShape {
private ArrayList<Shape> shapes;
public ArrayList<Shape> getShapes() {
return shapes;
}
public DisplayShape() {
shapes = new ArrayList<>();
}

View File

@ -0,0 +1,23 @@
package com.baeldung.interfaces.polymorphysim;
import java.util.function.Predicate;
public class FunctionalMain {
public static void main(String[] args) {
Shape circleShape = new Circle(2);
Shape squareShape = new Square(2);
DisplayShape DisplayShape = new DisplayShape();
DisplayShape.add(circleShape);
DisplayShape.add(squareShape);
Predicate<Shape> checkArea = (shape) -> shape.area() < 5;
for (Shape shape : DisplayShape.getShapes()) {
if (checkArea.test(shape)) {
System.out.println(shape.name() + " " + shape.area());
}
}
}
}

View File

@ -1,6 +1,9 @@
package com.baeldung.interfaces.polymorphysim;
public interface Shape {
import com.baeldung.interfaces.HasColor;
public interface Shape extends HasColor {
public abstract String name();
public abstract double area();
}

View File

@ -17,4 +17,9 @@ public class Square implements Shape {
public double area() {
return width * width;
}
@Override
public String getColor() {
return "red";
}
}