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:
parent
8fbed2e741
commit
21bde94745
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.interfaces;
|
||||
|
||||
public interface HasColor {
|
||||
public String getColor();
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.interfaces;
|
||||
|
||||
import com.baeldung.interfaces.multiinheritance.Transform;
|
||||
|
||||
public class Motorcycle implements Transform {
|
||||
@Override
|
||||
public void transform() {
|
||||
// Implementation
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.interfaces;
|
||||
|
||||
public class Truck extends Vehicle {
|
||||
@Override
|
||||
public void transform() {
|
||||
// implementation
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.interfaces;
|
||||
|
||||
import com.baeldung.interfaces.multiinheritance.Transform;
|
||||
|
||||
public abstract class Vehicle implements Transform {
|
||||
}
|
|
@ -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!!");
|
||||
}
|
||||
}
|
|
@ -1,5 +1,10 @@
|
|||
package com.baeldung.interfaces.multiinheritance;
|
||||
|
||||
public interface Transform {
|
||||
|
||||
void transform();
|
||||
|
||||
default void printSpecs(){
|
||||
System.out.println("Transform Specification");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,4 +18,8 @@ public class Circle implements Shape {
|
|||
return Math.PI * (radius * radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColor() {
|
||||
return "green";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,10 @@ public class DisplayShape {
|
|||
|
||||
private ArrayList<Shape> shapes;
|
||||
|
||||
public ArrayList<Shape> getShapes() {
|
||||
return shapes;
|
||||
}
|
||||
|
||||
public DisplayShape() {
|
||||
shapes = new ArrayList<>();
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -17,4 +17,9 @@ public class Square implements Shape {
|
|||
public double area() {
|
||||
return width * width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColor() {
|
||||
return "red";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue