Site icon TestingDocs.com

What are Sealed Classes in Java

Sealed Class

Sealed classes in Java allow inheritance from the known sub classes that are specified by the permits keyword or defined in the same class file. If we mark a class as sealed it should have some sub classes specified using the permits clause or sub classes defined in the file.  For example

public abstract sealed class Ship permits CargoShip,PassengerShip {

CargoShip and PassengerShip are the only child classes that can inherit Ship class.

CargoShip and PassengerShip child classes can be

Enable Preview

To enable Preview features in IntelliJ IDEA. Set Preview mode under the Project SDK.

Running a simple program or Java class using IntelliJ IDEA IDE. (https://www.testingdocs.com/simple-hello-world-program-using-intellij-ide/ )

or use command line switch:  –enable-preview

Let’s say by law NavelShip are not allowed as regular ships in the sea. So sealing the  class Ship to cargo and passenger ships. When we try to extend NavelShip as Ship class would result in error in the sealed class hierarchy.

 

 

permits

permits clause allows us to specify the child classes for the sealed classes. The child class can be specified in separate files.

// www.TestingDocs.com
public abstract sealed class Ship permits CargoShip,PassengerShip {
    protected String name;

    public Ship(String name) {
        this.name = name;
    }

}

PassengerShip and CargoShip can inherit the ship class.

public final class PassengerShip extends Ship{
    private int noOfPassengers;

    public PassengerShip(String name,int p) {
        super(name);
        this.noOfPassengers=p;
    }

    public int getNoOfPassengers() {
        return noOfPassengers;
    }

    public void setNoOfPassengers(int noOfPassengers) {
        this.noOfPassengers = noOfPassengers;
    }

    @Override
    public String toString() {
        return "Passenger Ship{" +
                "Cargo Weight in Tons=" + getNoOfPassengers()  +
                ", name='" + name + '\'' +
                '}';
    }
}

Note that Passenger ship is final class. So other classes cannot inherit from the final class PassengerShip. Sealed classes can extend other sealed classes. For example, due to the fewer number of Passenger ships government allows warships to carry passengers due to emergency.

 

public sealed class PassengerShip extends Ship permits WarShip{

public sealed class PassengerShip extends Ship permits WarShip{
    private int noOfPassengers;

    public PassengerShip(String name,int p) {
        super(name);
        this.noOfPassengers=p;
    }

    public int getNoOfPassengers() {
        return noOfPassengers;
    }

    public void setNoOfPassengers(int noOfPassengers) {
        this.noOfPassengers = noOfPassengers;
    }

    @Override
    public String toString() {
        return "Passenger Ship{" +
                "Number of passengers=" + getNoOfPassengers()  +
                ", name='" + name + '\'' +
                '}';
    }
}

 

non-sealed

non-sealed opens up the inheritance to unknown child classes. This is like the normal inheritance in Java without specifying the keyword. or example, a non-sealed class would open the sealed hierarchy by allowing unknown classes to inherit the class.

 

//www.TestingDocs.com
public non-sealed class CargoShip extends Ship {
    private double cargoWeight; // kgs

    public CargoShip(String name, double w) {
        super(name);
        this.cargoWeight=w;
    }

    protected double convertToTons() {
        return this.cargoWeight/1000;
    }

    @Override
    public String toString() {
        return "Cargo Ship{" +
                "Cargo Weight in Tons=" + convertToTons()  +
                ", name='" + name + '\'' +
                '}';
    }
}

 

Exit mobile version