Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between final Class and Class?

Tags:

swift

What is the difference between final Class and Class?

final class A {  }  class B {      } 
like image 617
Mohammad Razipour Avatar asked Sep 05 '17 07:09

Mohammad Razipour


People also ask

What is the difference between a final class and other classes?

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.

What does final class mean?

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 .)

What is the difference between final class and private class?

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.

What is the difference between abstract class and 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.


1 Answers

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.

like image 195
luckyShubhra Avatar answered Oct 05 '22 00:10

luckyShubhra