Showing posts with label sealed interface. Show all posts
Showing posts with label sealed interface. Show all posts

Thursday, 28 July 2022

Sealed classes, interfaces and records

Sealed classes, interfaces work well with records. To demonstrate the example, I am defining a sealed interface Operation, and two record types Add, Mul will implement this sealed interface.

 


Operation.java

package com.sample.app.recrods;

public sealed interface Operation permits Add, Mul{

    public Long operation();
}

Add.java

package com.sample.app.recrods;

public record Add(int... a) implements Operation {

    @Override
    public Long operation() {
        if(a == null || a.length == 0) {
            return null;
        }
        
        long sum = 0;
        
        for(int ele : a) {
            sum += ele;
        }
        
        return sum;
    }

}

Mul.java

package com.sample.app.recrods;

public record Mul(int... a) implements Operation {

    @Override
    public Long operation() {
        if (a == null || a.length == 0) {
            return null;
        }

        long result = 1;

        for (int ele : a) {
            result *= ele;
        }

        return result;
    }

}

SealedAndRecordDemo.java

package com.sample.app;

import com.sample.app.recrods.Add;
import com.sample.app.recrods.Mul;

public class SealedAndRecordDemo {

    public static void main(String[] args) {
        Add add = new Add(new int[] { 2, 3, 5, 7 });
        Mul mul = new Mul(new int[] { 2, 3, 5, 7 });

        System.out.println("Sum of first four primes : %d".formatted(add.operation()));
        System.out.println("Multiplication of first four primes : %d".formatted(mul.operation()));
    }

}

Output

Sum of first four primes : 17
Multiplication of first four primes : 210

 

References

https://openjdk.org/jeps/360

https://openjdk.org/jeps/384

 



 

Previous                                                 Next                                                 Home

Java15: Sealed interfaces

This is continuation to my previous post ‘sealed classes’, I would request you to go through the previous post before reading this.

 

Sealed classes and interfaces specify who can extend or implement this class or interface. A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.

 

A interface is sealed by applying the sealed modifier to its declaration. For example, the following declaration declare a sealed interface EmployeeService which permits two classes (PermanentEmployeeServiceImpl,TemporaryEmployeeServiceImpl) to implement it.

 

EmployeeService.java

package com.sample.app.interfaces.sealed;

public sealed interface EmployeeService permits PermanentEmployeeServiceImpl,TemporaryEmployeeServiceImpl {

}

 

PermanentEmployeeServiceImpl.java

package com.sample.app.interfaces.sealed;

public final class PermanentEmployeeServiceImpl implements EmployeeService{

}

 

TemporaryEmployeeServiceImpl.java

package com.sample.app.interfaces.sealed;

public final class TemporaryEmployeeServiceImpl implements EmployeeService{

}

 

Where to specify the permit clause, if my interface extends another interfaces?

After any extends clause to specify superinterfaces, the implementing classes and subinterfaces are specified with a permits clause. 

public sealed interface PersonInterface extends Serializable permits PersonImpl, UserImpl{

}

  

References

https://openjdk.org/jeps/360

 

 

Previous                                                 Next                                                 Home