Unit-3 Java New Features
Unit-3 Java New Features
ENT OF CSE-IOT)
Prof. SHIV VEER SINGH
Abstract Methods
• Abstract methods are methods declared in an abstract class or
in an interface without an implementation. These methods
must be implemented by subclasses or implementing classes.
An abstract method provides a way to enforce certain
behaviors in the subclasses or implementing classes.
ENT OF CSE-IOT)
interfaces.
• Subclasses of an abstract class must provide implementations
for all abstract methods unless the subclass is also abstract.
• Interfaces in Java can have abstract methods by default (before
Java 8).
• package Unit3;
• abstract class A {
• abstract void am(); // Abstract method (no body)
• void rm() { // Regular method
• System.out.println("Regular method");
• }
• }
• abstract class D extends A { // Providing implementation of abstract method
• void am() {
• System.out.println("Abstract method Implementation of class A");
ENT OF CSE-IOT)
• void am2() {
• System.out.println("Abstract method Implementation of class D");
• }
• }
• public class MainClass {
• public static void main(String[] args) {
• E e = new E();
• e.am();
• e.rm();
• e.am2();
• }
Default Methods
• Default methods were introduced in Java 8 with in interfaces.
These methods have a default implementation and can be
overridden by implementing classes. Default methods allow
developers to add new methods to interfaces without
breaking the existing implementations of those interfaces.
ENT OF CSE-IOT)
• They help in providing backward compatibility.
• package Unit3;
• interface I {
• void am(); // Abstract method
• default void dm() { // Default method
• System.out.println("Default method Calling");
• }
• }
• class DM implements I {
• public void am() { // Providing implementation of abstract method
ENT OF CSE-IOT)
• }
• }
• public class DefaultMethodMainClass {
• public static void main(String[] args) {
• DM d = new DM();
• d.am();
• d.dm();
• }
Static Methods
• Static methods are methods that belong to the class rather
than any instance of the class. They can be called without
creating an instance of the class. Static methods are often used
for utility or helper methods.
• Note:
ENT OF CSE-IOT)
methods directly.
• They cannot access instance variables or instance methods
directly.
• class A{
• static int add(int a, int b) { // Static method
• return a + b;
• }
• }
• public class Main {
ENT OF CSE-IOT)
• System.out.println("Sum: " + sum); }
• }
Functional Interfaces
• A functional interface is an interface that has exactly one abstract
method. These interfaces are used as the basis for lambda expressions
and method references in Java.
• Functional Interface is also known as Single Abstract Method
Interfaces or SAM Interfaces. It is a new feature in Java, which helps
ENT OF CSE-IOT)
2. A functional interface can extends another interface only when it
does not have any abstract method.
3. It Must have exactly one abstract method.
4. It Can have multiple default or static methods.
5. The @FunctionalInterface annotation can be used to mark an
interface as a functional interface.
• Ex.1
• @FunctionalInterface
• interface MYFunctionalInterface{
• void am(String s);
• }
• public class FunctionalInterfaceExample implements MYFunctionalInterface{
• public void am(String s){
ENT OF CSE-IOT)
FunctionalInterfaceExample();
• fie.am("MY Functional Interface Example");
• }
• }
Ex.2
• @FunctionalInterface
• interface MYFunctionalInterface{
• void am(String s);
• int hashCode();
• String toString();
• boolean equals(Object obj);
ENT OF CSE-IOT)
• public void am(String s){
• System.out.println(msg);
• }
• public static void main(String[] args) {
• FunctionalInterfaceExample fie = new FunctionalInterfaceExample();
• fie.am("MY Functional Interface Example");
• }
• }
Lambda Expressions
• A lambda expression is a concise way to represent an anonymous
function (i.e., a function without a name) that can be passed
around. It provides a clear and concise way to implement a method
of a functional interface.
• Note:
ENT OF CSE-IOT)
is an interface which has only one method. The lambda expression
should have the same number of parameters and the same return
type as that method. Java has many of these kinds of interfaces
built in, such as the Consumer interface (found in
the java.util package) used by lists.
Lambda Expression Parameters
There are three Lambda Expression Parameters:
1.Zero Parameter
ENT OF CSE-IOT)
() -> System.out.println("Zero parameter lambda");
ENT OF CSE-IOT)
• Consumer<Integer> method = (n) -> { System.out.println(n); };
num.forEach( method );
• }
• }
Method reference
Method reference is a special type of lambda expression. It fulfils the
purpose of a lambda expression by increasing the readability and to write a
neat code.
A method reference works in case of functional interfaces. Wherever, we are
calling a method of a functional interface with a lambda expression, we can
ENT OF CSE-IOT)
"::" symbol.
Types of Java Method References
A method reference can be used to point the
following types of methods −
1. Static methods
2. Instance methods
3. Constructors using new operator
ENT OF CSE-IOT)
Method Reference for Static Method
A static method can be referred using "::" symbol
easily without creating any instance of the class,
and by using class name.
Ex.
• public class MethodReference {
• num.add(3);
• num.add(6);
• num.add(2);
• num.add(7);
ENT OF CSE-IOT)
• System.out.println(n);
• }
ENT OF CSE-IOT)
• Pipelining − Most of the stream operations return stream itself
so that their result can be pipelined. These operations are
called intermediate operations and their function is to take
input, process them, and return output to the target. collect()
method is a terminal operation which is normally present at the
end of the pipelining operation to mark the end of the stream.
Generating Streams in Java
• With Java 8, Collection interface has two methods to generate a
Stream.
• stream() − Returns a sequential stream considering collection as
its source.
• parallelStream() − Returns a parallel Stream considering
collection as its source.
ENT OF CSE-IOT)
Important Methods for Stream API
1. forEach Method
• Stream has provided a new method 'forEach' to iterate each
element of the stream. The following code segment shows
how to print 10 random numbers using forEach.
• Example:
ENT OF CSE-IOT)
map Method
• The 'map' method is used to map each element to its
corresponding result. The following code segment prints
unique squares of numbers using map.
• Example:
ENT OF CSE-IOT)
filter Method
• The 'filter' method is used to eliminate elements based on a
criteria. The following code segment prints a count of empty
strings using filter.
• Example:
ENT OF CSE-IOT)
• int count = strings.stream().filter(string -> string.isEmpty()).count();
PROF. SHIV VEER SINGH (DEPARTM
ENT OF CSE-IOT)
Base64 Encode and Decode,
• Base64 encoding is a method to encode binary data into an
ASCII string format using a base-64 digit representation. It is
commonly used for encoding data to be sent over media that
are designed to deal with text. This ensures that the data
remains intact without modification during transport. Base64
encoding uses 64 characters (A-Z, a-z, 0-9, +, /) plus the "="
ENT OF CSE-IOT)
• Base64 Encoding:Convert binary data (like a byte array) into a
Base64 encoded string.
• Base64 Decoding:Convert a Base64 encoded string back into
binary data (like a byte array).
Example:
• import java.util.Base64;
ENT OF CSE-IOT)
•
• // Decoding the Base64 encoded string back to original string
• byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
• String decodedString = new String(decodedBytes);
• System.out.println("Decoded String: " + decodedString);
• }
• }
• Encoding:
• Base64.getEncoder().encodeToString(input.getBytes()):
• input.getBytes() converts the string input to a byte array.
• Base64.getEncoder().encodeToString(...) encodes the byte array
to a Base64 string.
• Decoding:
ENT OF CSE-IOT)
• new String(decodedBytes) converts the byte array back to a
string.
• The Base64 class also provides methods for encoding and
decoding URLs and MIME types:
• URL Encoding and Decoding:
• Base64.getUrlEncoder()
• Base64.getUrlDecoder()
• MIME((Multipurpose Internet Mail Extensions)) Encoding and
Decoding:
ENT OF CSE-IOT)
URL Encoding and Decoding:
• URL encoding is used to encode data that will be included in URLs. URL-
safe encoding replaces + and / with - and _ respectively to ensure the
encoded string can be safely included in a URL.
• import java.util.Base64;
ENT OF CSE-IOT)
• System.out.println("URL Encoded String: " + urlEncodedString);
ENT OF CSE-IOT)
• System.out.println("MIME Encoded String: " + mimeEncodedString);
ENT OF CSE-IOT)
• Repeating annotations allow the same annotation to be
applied multiple times to a declaration or type use.
Diamond Syntax with Inner Anonymous Class
• Java 7 introduced the diamond syntax (`<>`) to reduce boilerplate code when
creating generic instances. This can also be used with inner anonymous classes.
ENT OF CSE-IOT)
• };
•
• for (String s : list) {
• System.out.println(s);
• }
• }
• }
Local Variable Type Inference
• Java 10 introduced the `var` keyword, which allows the
compiler to infer the type of a local variable.
• public class LocalVariableTest {
• public static void main(String[] args) {
•
ENT OF CSE-IOT)
• System.out.println(message);
• System.out.println(number);
Text Blocks
•
• Text blocks (introduced in Java 13) make it easier to write multiline
strings.
• public class TextBlocksTest{
• public static void main(String[] args) {