Why Java Predicate contains implementation methods like not, or, and?

74 Views Asked by At

While looking into the source code of Predicate interface in java I found, it contains few implemented methods like or, and, isEqual. But how a interface can contain non abstract methods?

@FunctionalInterface
public interface Predicate<T> {

    
    boolean test(T t);

  
    default Predicate<T> **and**(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

   
    default Predicate<T> **negate**() {
        return (t) -> !test(t);
    }

    
    default Predicate<T> **or**(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    
    static <T> Predicate<T> **isEqual**(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }

    @SuppressWarnings("unchecked")
    static <T> Predicate<T> **not**(Predicate<? super T> target) {
        Objects.requireNonNull(target);
        return (Predicate<T>)target.negate();
    }
    }

Could anyone please explain how an interface can contain implemented methods in it?

Thanks

0

There are 0 best solutions below