# Java 8 Features - Demos
This document covers various Java 8 features with code examples and explanations.
---
## Demo 01 - Java 8 Interfaces (`@FunctionalInterface`)
Java 8 allows writing static methods in interfaces, eliminating the need for additional helper/utility classes.
### Key Features:
- **Static methods**
- **Default methods**
- **Interface `Shape`**
- `calcArea()`
- `calcPeri()`
- `calcTotalArea()`
---
## Demo 02 - Java 8 Interfaces
### Multiple Interface Inheritance
- **Interface `Printable`** - `show()`
- **Interface `Displayable`** - `show()`
- In Java 7, **no ambiguity** arises because interfaces don’t contain method definitions.
### Default Methods & Ambiguity
- If two interfaces have a default method with the **same signature** and a class inherits from both, it causes **ambiguity**.
- **Solution:** Override the method in the subclass.
### Super Class Wins Over Super-Interfaces
- If a class inherits the same method signature from both a **superclass** and a **super-interface**, the **superclass method takes precedence**.
- No compiler error for ambiguity.
### Method Overriding
- Method is called depending on the **type of the object**.
---
## Demo 03 - Lambda Expressions
### Sorting Using Comparator
```java
Employee[] arr = new Employee[] {
new Employee(4, "B", "Clerk", "Sales", 723.44),
new Employee(8, "X", "Manager", "Accounts", 823.23),
new Employee(2, "P", "Clerk", "Research", 234.23),
new Employee(9, "N", "Manager", "Sales", 252.53),
new Employee(5, "D", "Clerk", "Accounts", 923.23),
new Employee(1, "Q", "Analyst", "Research", 826.23),
new Employee(7, "H", "Clerk", "Research", 845.24),
new Employee(6, "A", "Analyst", "Research", 832.23),
new Employee(3, "G", "Analyst", "Sales", 952.44)
};
.png)
.png)
.png)
.png)
.png)
.png)
Sorting Methods:
- EmployeeIdComparator – Local Class
- Sort by Name — Anonymous Inner Class (
empNameComparator
)
- Sort by Job — Anonymous Inner Class
- Sort by Job (Descending)
- Sort by Name (Descending) - Type Inference
- Sort by Salary (Ascending)
- Sort by Salary (Descending)
- Sort by Employee ID (Descending)
Functional Interfaces & Lambda Expressions:
- Predefined Functional Interfaces
- Using
BinaryOperator
- Lambda expressions are referenced by functional interface reference.