Skip to main content

Posts

Showing posts from September, 2025

Java 25: Flexible Constructors

Java 25 Features Java 25 Example: FlexibleConstructors In Java 8 /*In java 8 the super constructor is needed to be the first call in child class constructor (if needed)*/ Super constructor gets first call in child constructor, this is not flexible-code, that means if super class is getting initialized even before initializing the child class, if child class initialization failed then super class constructor is created and will be useless. Copy public class FlexibleConstructors { public static void main(String[] args) { try { Cat cat = new Cat(true, "Russian Bob Cat"); System.out.println("Animal Details: " + cat.breed + "--" + cat.domestic); Cat lion = new Cat(false, "African Lion"); System.out.println("Animal Details: " + li...

Java 25 Feature: Flexible Code Compilation by Pran Sukh

Java 25 Features Java 25 Example: Primitive Pattern Matching Copy /* In java 25 you need explicit definition of class and main method, just for start it's easy to understand the execution. * But in real Object-Oriented applications you cannot go without classes and inheritance */ void main(){ int i = 3; char any = 'P'; oldWayOfUnBoxing(i); oldWayOfUnBoxing(any); checkObject(i); checkObject(any); } static void oldWayOfUnBoxing(Object obj) { if (obj instanceof Integer) { int i = ((Integer) obj).intValue(); // First obj is boxed to Integer then unboxed to int with .intValue of Integer class log("Primitive obj of type int is converted/unboxed to int with Integer class"); } if (obj instanceof Character) { char c = ((Character) obj).charValue(); // First ob...

Java 25 Sealed Classes and Interfaces

Sealed Classes and Interfaces in Java 25 | Complete Guide Mastering Sealed Classes and Interfaces in Java 25 With the release of Java 25 , developers now have more control over inheritance hierarchies through the use of sealed interfaces and sealed classes . This feature is a big step in strengthening object-oriented programming in Java. 🔍 What are Sealed Interfaces and Classes? Sealed types in Java allow developers to specify which classes or interfaces are allowed to extend or implement a given type. This enables controlled extension, useful in domain modeling , security, and compiler optimizations . 📌 Java 25 Code Example: Modeling a Cat Hierarchy 1. Sealed Interface: SIAnimal This sealed interface can only be implemented by the SCat class. 2. Sealed Abstract Class: SCat The abstract class SCat can only be extended by the listed subclasses: Lion , Tiger , BobCat , and Leapord . 3. Final Class: Lion ...