What is the difference between final Class and Class?
final class A { } class B { }
The main purpose of using a final class is to prevent the class from being inherited (i.e.) if a class is marked as final, then no other class can inherit any properties or methods from the final class. If the final class is extended, Java gives a compile-time error.
A final class is simply a class that can't be extended. (It does not mean that all references to objects of the class would act as if they were declared as final .)
The main difference between private and final class is that you can inherit from a private class in the same class but extending the final class is prohibited by the Java compiler. Though you can not create a sub-class of a private class outside of the class they are declared, similar to the final class.
The main difference between abstract class and final class in Java is that abstract class is a class with abstract and non-abstract methods, which allows accomplishing abstraction, while final class is a class that restricts the other classes from accessing its data and methods.
Final is class modifier which prevents it from being inherited or being overridden. From apple documentation
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript’s introducer keyword (such as final var, final func, final class func, and final subscript).
Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
You can mark an entire class as final by writing the final modifier before the class keyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With