Default and static methods in Java
Default methods of an interface
Java 8 allows you to add non-abstract method implementations to an interface using the default keyword:
interface Example {
int process(int a);
default void show() {
System.out.println("default show()");
}
}
- If a class implements an interface, it can, but does not have to, implement the default methods already implemented in the interface. The class inherits the default implementation.
- If a class implements multiple interfaces that have the same default method, then the class must implement the method with the same signature on its own. The situation is similar if one interface has a default method, while in another the same method is abstract - no default implementation is inherited by the class.
- The default method cannot override the java.lang.Object class method.
- Helps to implement interfaces without fear of breaking other classes.
- They avoid creating utility classes, since all the necessary methods can be represented in the interfaces themselves.
- Give the classes freedom to choose the method to override.
- One of the main reasons for implementing default methods is Java 8's ability to use lambda expressions.
How to call the default method of an interface in a class that implements this interface?
Using the super keyword along with the interface name:
interface Paper {
default void show() {
System.out.println("default show()");
}
}
class License implements Paper {
public void show() {
Paper.super.show();
}
}
Static method of an interface
Static methods of an interface are similar to the default methods, except that they cannot be overridden in classes that implement the interface.
- Static methods in an interface are part of the interface without the ability to use them for objects of the implementation class;
- The methods of the java.lang.Object class cannot be overridden as static;
- Static methods in an interface are used to provide helper methods such as checking for null, sorting collections, etc.
How do I call a static method of an interface?
Using the interface name:
interface Paper {
static void show() {
System.out.println("static show()");
}
}
class License {
public void showPaper() {
Paper.show();
}
}
Optional in Java
Optional is a container for an object, which may or may not contain null. This wrapper is a convenient way to prevent NullPointerException, since has some higher-order functions that get rid of the addition of repeated if null/notNull checks:
Optional<String> optional = Optional.of("hello");
optional.isPresent(); // true
optional.ifPresent(s -> System.out.println(s.length())); // 5
optional.get(); // "hello"
optional.orElse("ops..."); // "hello"
StringJoiner in Java
The StringJoiner class is used to create a sequence of delimited strings with the option to prefix and suffix the resulting string:
StringJoiner joiner = new StringJoiner(".", "prefix-", "-suffix");
for (String s : "Hello the brave world".split(" ")) {
joiner.add(s);
}
System.out.println(joiner); //prefix-Hello.the.brave.world-suffix
Read also:
Comments
Post a Comment