1)Final keyword?(Explain string as immutable)
- First of all, final is a non-access modifier applicable only to a variable, a method or a class.Following are different contexts where final is used.
- Final variables. When a variable is declared with final keyword, its value can't be modified, essentially, a constant.
Final variable:
We can declare in instance variable , static block,local variable and method argument(constructor also),loop and switch case also.
- When a variable is declared with final keyword, its value can’t be modified, essentially, a constant.
- This also means that you must initialize a final variable. If the final variable is a reference, this means that the variable cannot be re-bound to reference another object, but internal state of the object pointed by that reference variable can be changed i.e. you can add or remove elements from final array or final collection.
- It is good practice to represent final variables in all uppercase, using underscore to separate words.
- when you try to modify value "the final local variable a may already have been assigned";
- When you try to increment/decrements following error will display from the compiler
"The final local variable a cannot be assigned. It must be blank and not using a compound assignment"
Examples :
// a final variable final int THRESHOLD = 5; // a blank final variable final int THRESHOLD; // a final static variable PI static final double PI = 3.141592653589793; // a blank final static variable static final double PI;
Final classes
When a class is declared with final keyword, it is called a final class. A final class cannot be extended(inherited). There are two uses of a final class :
- One is definitely to prevent inheritance, as final classes cannot be extended. For example, all Wrapper Classes like Integer,Float etc. are final classes. We can not extend them.
final class A { // methods and fields } // The following class is illegal. class B extends A { // COMPILE-ERROR! Can't subclass A }
- The other use of final with classes is to create an immutable class like the predefined String class.You can not make a class immutable without making it final.
Final methods
When a method is declared with final keyword, it is called a final method. A final method cannot be overridden. The Object class does this—a number of its methods are final.We must declare methods with final keyword for which we required to follow the same implementation throughout all the derived classes. The following fragment illustrates final keyword with a method:
class A { final void m1() { System.out.println("This is a final method."); } } class B extends A { void m1() { // COMPILE-ERROR! Can't override. System.out.println("Illegal!"); } }
For more examples and behavior of final methods and final classes, please see Using final with inheritance
final vs abstract
Following table gives the differences: abstract class vs final class.
PROPERTY | ABSTRACT CLASS | FINAL CLASS |
---|---|---|
SUBCLASSING | Should be subclassed to override the functionality of abstract methods | Can never be subclassed as final does not permit |
METHOD ALTERATIONS | Abstract class methods functionality can be altered in subclass | Final class methods should be used as it is by other classes |
INSTANTIATION | Cannot be instantiated | Can be instantiated |
OVERRIDING CONCEPT | For later use, all the abstract methods should be overridden | Overriding concept does not arise as final class cannot be inherited |
INHERITANCE | Can be inherited | Cannot be inherited |
ABSTRACT METHODS | Can contain abstract methods | Cannot contain abstract methods |
PARTIAL IMPLEMENTATION | A few methods can be implemented and a few cannot | All methods should have implementation |
IMMUTABLE OBJECTS | Cannot create immutable objects (infact, no objects can be created) | Immutable objects can be created (eg. String class) |
NATURE | It is an incomplete class (for this reason only, designers do not allow to create objects) | It is a complete class (in the sense, all methods have complete functionality or meaning) |
ADDING EXTRA FUNCTIONALITY | Extra functionality to the methods can be added in subclass | No extra functionality can be added and should be used as it is |
2)
No comments:
Post a Comment