Wednesday, July 23, 2014

final keyword in all languages



You are always allowed to initialize a final variable. The compiler makes sure that you can do it only once.
Note that calling methods on an object stored in a final variable has nothing to do with the semantics of final. In other words: final is only about the reference itself, and not about the contents of the referenced object.
Java has no concept of object immutability; this is achieved by carefully designing the object, and is a far-from-trivial endeavor.
final class cannot be subclassed
final methods cannot be overridden. (This method is in superclass)
final methods can override. (Read this in grammatical way. This method is in subclass)
A final variable can only be initialized once
Final are:-
·         Variable
·         Class
·         Method

1) final variable
If you make any variable as final, you cannot change the value of final variable(It will be constant).

2) final method

If you make any method as final, you cannot override it.

3) final class

If you make any class as final, you cannot extend it.

Q) Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:


Q) What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known as blank final variable.
If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in constructor.

Que) Can we initialize blank final variable?

Yes, but only in constructor

What is final parameter?

If you declare any parameter as final, you cannot change the value of it.

Can we declare a constructor final?

No, because constructor is never inherited.

No comments:

Post a Comment