Tuesday 22 January 2019

ITV-Final keyword in java

1)Final keyword?(Explain string as immutable)

  1. 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.
  2.  Final variables. When a variable is declared with final keyword, its value can't be modified, essentially, a constant.

final keyword in java
Final variable:
We can declare in instance variable , static block,local variable and method argument(constructor also),loop and switch case also.
  1. When a variable is declared with final keyword, its value can’t be modified, essentially, a constant.
  2.  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. 
  3. It is good practice to represent final variables in all uppercase, using underscore to separate words.
  4. when you try to modify value "the final local variable a may already have been assigned";
  5. 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 :
  1. 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
    }
    
  2. 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.
PROPERTYABSTRACT CLASSFINAL CLASS
SUBCLASSINGShould be subclassed to override the functionality of abstract methodsCan never be subclassed as final does not permit
METHOD ALTERATIONSAbstract class methods functionality can be altered in subclassFinal class methods should be used as it is by other classes
INSTANTIATIONCannot be instantiatedCan be instantiated
OVERRIDING CONCEPTFor later use, all the abstract methods should be overriddenOverriding concept does not arise as final class cannot be inherited
INHERITANCECan be inheritedCannot be inherited
ABSTRACT METHODSCan contain abstract methodsCannot contain abstract methods
PARTIAL IMPLEMENTATIONA few methods can be implemented and a few cannotAll methods should have implementation
IMMUTABLE OBJECTSCannot create immutable objects (infact, no objects can be created)Immutable objects can be created (eg. String class)
NATUREIt 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 FUNCTIONALITYExtra functionality to the methods can be added in subclassNo extra functionality can be added and should be used as it is
2)

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...