Java-Final to Prevent Inheritance

Final to Prevent Inheritance

A class that is declared with the final modifier cannot be extended. Sometimes we may like to prevent a class being further subclasses for security reasons. A class that cannot be subclassed is called a final class. Any attempt to inherit these classes will cause an error and the compiler will not allow it.

Program

Program Source

final class DataV1 {
    
    
}

class DataV2 extends DataV1{
    
    
}

public class Javaapp {
    
    public static void main(String[] args){
        
        DataV2 obj = new DataV2();
   }
}

Leave a Comment