0% found this document useful (0 votes)
4 views3 pages

Sealed Classes and Functional Interface in Java

Uploaded by

brilliantyx
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Sealed Classes and Functional Interface in Java

Uploaded by

brilliantyx
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Lab internal 4B1

Name – Harsh Vardhan Singh


Roll No. - 2200290110075

WAP to implement sealed classes with functional interface.

Code – :

sealed class Shape permits Circle, Rectangle {

// common shape methods can be defined here if needed

final class Circle extends Shape {

private final double radius;

public Circle(double radius) {

[Link] = radius;

public double getRadius() {

return radius;

final class Rectangle extends Shape {

private final double width;


private final double height;

public Rectangle(double width, double height) {

[Link] = width;

[Link] = height;

public double getWidth() {

return width;

public double getHeight() {

return height;

@FunctionalInterface

interface ShapeOperation {

double calculate(Shape shape);

public class ShapeCalculator {

public static void main(String[] args) {

ShapeOperation areaOperation = (Shape shape) -> {

if (shape instanceof Circle) {

Circle circle = (Circle) shape;

return [Link] * [Link]() * [Link]();

} else if (shape instanceof Rectangle) {

Rectangle rectangle = (Rectangle) shape;


return [Link]() * [Link]();

throw new IllegalArgumentException("Unknown shape");

};

Shape circle = new Circle(5);

Shape rectangle = new Rectangle(4, 6);

[Link]("Circle area: " + [Link](circle));

[Link]("Rectangle area: " +


[Link](rectangle));

Output : -

Circle area: 78.53981633974483

Rectangle area: 24.0

Common questions

Powered by AI

Lambda expressions enhance readability by allowing inline implementation of methods defined by the ShapeOperation interface without verbosity. It directly links the implementation with the functional interface use case, making the purpose clear and concise. Further, it improves efficiency by reducing overhead related to boilerplate code for anonymous classes, simplifying both writing and understanding the executing logic .

Sealed classes in Java restrict which classes can subclass a particular class, providing better control over the inheritance hierarchy. This ensures that the Shape class can only be extended by the specified subclasses, Circle and Rectangle, preventing unauthorized extensions that could introduce errors or unexpected behavior. This enhances maintainability and enforces stricter design patterns .

The throw statement efficiently handles unexpected shape types by terminating the program and alerting developers of the error via IllegalArgumentException. This explicit handling provides clear feedback, aiding in debugging. However, it requires developers to ensure that only expected Shape types are ever passed, highlighting the importance of controlled system inputs .

The 'final' keyword in the class definitions of Circle and Rectangle indicates these classes cannot be extended further, conforming to the sealed class hierarchy of the Shape class. This prevents further subclassing, ensuring the hierarchy remains confined to the specified set of classes, thereby maintaining controlled access and consistency in the system's design .

Removing 'instanceof' checks would lead to runtime errors if a Shape that is neither a Circle nor a Rectangle were passed, as there would be no logic to handle such cases. This could throw an IllegalArgumentException, leading to potential system crashes if not handled correctly. It would undermine type safety and the robustness of the application .

To enhance extensibility, the ShapeCalculator class could adopt a more dynamic approach by leveraging a Map to associate shape types with calculation strategies. This would allow new shapes to register their calculation logic at runtime without modifying existing code, adhering to the open-closed principle. Additional refactoring might involve creating a method in each Shape subclass to return its area, encapsulating logic within the class structure .

Using a functional interface like ShapeOperation allows for the definition of a single abstract method, providing flexibility by enabling lambda expressions or method references to define their behavior. This design supports functional programming paradigms, separating the operation’s logic from the Shape objects themselves, thus helping achieve cleaner and more modular code .

Encapsulation in the Circle and Rectangle classes is achieved by marking fields as private and providing controlled access via public getters. This encapsulation ensures that the internal state cannot be directly modified from outside the class, protecting against inconsistent states and unintended side effects, thereby preserving the integrity and security of the objects’ data .

Polymorphism in ShapeCalculator is achieved through the ShapeOperation functional interface and the conditional checks within the lambda expression. Shape references are abstract, referring to either Circle or Rectangle instances, yet the lambda function determines the specific execution path by checking the actual type using 'instanceof'. This allows different area calculation logic to be executed based on the actual type of Shape without rewriting code for each subclass .

The implementation balances inheritance by using sealed classes to create a simple hierarchy (Shape -> Circle, Rectangle) ensuring controlled inheritance and composition through the use of interfaces (ShapeOperation) to separate behavior logic from the data structure. This approach advocates flexibility and reusability of behavior across different shape types while ensuring modular and manageable code .

You might also like