diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/Electronic.java b/core-java-8/src/main/java/com/baeldung/interfaces/Electronic.java new file mode 100644 index 0000000000..bfbc381483 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/Electronic.java @@ -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"); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/Employee.java b/core-java-8/src/main/java/com/baeldung/interfaces/Employee.java new file mode 100644 index 0000000000..903bc81e6f --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/Employee.java @@ -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; + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java b/core-java-8/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java new file mode 100644 index 0000000000..cfa4226c1a --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java @@ -0,0 +1,18 @@ +package com.baeldung.interfaces; + +import java.util.Comparator; + +public class EmployeeSalaryComparator implements Comparator { + + @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; + } + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/HasColor.java b/core-java-8/src/main/java/com/baeldung/interfaces/HasColor.java new file mode 100644 index 0000000000..6eface2d47 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/HasColor.java @@ -0,0 +1,5 @@ +package com.baeldung.interfaces; + +public interface HasColor { + public String getColor(); +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/Motorcycle.java b/core-java-8/src/main/java/com/baeldung/interfaces/Motorcycle.java new file mode 100644 index 0000000000..6003f476a3 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/Motorcycle.java @@ -0,0 +1,10 @@ +package com.baeldung.interfaces; + +import com.baeldung.interfaces.multiinheritance.Transform; + +public class Motorcycle implements Transform { + @Override + public void transform() { + // Implementation + } +} \ No newline at end of file diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/Truck.java b/core-java-8/src/main/java/com/baeldung/interfaces/Truck.java new file mode 100644 index 0000000000..d78de23371 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/Truck.java @@ -0,0 +1,8 @@ +package com.baeldung.interfaces; + +public class Truck extends Vehicle { + @Override + public void transform() { + // implementation + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/Vehicle.java b/core-java-8/src/main/java/com/baeldung/interfaces/Vehicle.java new file mode 100644 index 0000000000..8b4662e1a3 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/Vehicle.java @@ -0,0 +1,6 @@ +package com.baeldung.interfaces; + +import com.baeldung.interfaces.multiinheritance.Transform; + +public abstract class Vehicle implements Transform { +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java new file mode 100644 index 0000000000..b951fc0273 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java @@ -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!!"); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java index a18bbafdc1..8bdba43a05 100644 --- a/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java +++ b/core-java-8/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java @@ -1,5 +1,10 @@ package com.baeldung.interfaces.multiinheritance; public interface Transform { + void transform(); + + default void printSpecs(){ + System.out.println("Transform Specification"); + } } diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java index bf0e613567..afb3142d96 100644 --- a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java @@ -18,4 +18,8 @@ public class Circle implements Shape { return Math.PI * (radius * radius); } + @Override + public String getColor() { + return "green"; + } } diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java index 2cf4fafee1..d9c9dd107a 100644 --- a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/DisplayShape.java @@ -6,6 +6,10 @@ public class DisplayShape { private ArrayList shapes; + public ArrayList getShapes() { + return shapes; + } + public DisplayShape() { shapes = new ArrayList<>(); } diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/FunctionalMain.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/FunctionalMain.java new file mode 100644 index 0000000000..5316dd7db7 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/FunctionalMain.java @@ -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 checkArea = (shape) -> shape.area() < 5; + + for (Shape shape : DisplayShape.getShapes()) { + if (checkArea.test(shape)) { + System.out.println(shape.name() + " " + shape.area()); + } + } +} +} diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java index fcb0c65e7b..560e07a80a 100644 --- a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java @@ -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(); } diff --git a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java index 9c440150b5..00b75ace20 100644 --- a/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java +++ b/core-java-8/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java @@ -17,4 +17,9 @@ public class Square implements Shape { public double area() { return width * width; } + + @Override + public String getColor() { + return "red"; + } }