top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

what is final class,final variable, final method in java?

+2 votes
420 views
what is final class,final variable, final method in java?
posted Dec 29, 2014 by Shyam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Final Class:

If a class is declared as final, it cannot be inherited by derived classes.

final variable:

If a variable is declared as final, it can initialized only once. Its not mandatory to initialize the final variable at the time of declaration. If its not initialized at the time of declaration, it called blank final variable.

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

class Bike9{  
 final int speedlimit=90;//final variable  
 void run(){  
  speedlimit=400;  
 }  
 public static void main(String args[]){  
 Bike9 obj=new  Bike9();  
 obj.run();  
 }  
}//end of class 

**Output** :Compile Time Error

Final Method:

If a method is declared as final, it cannot be overridden by derived classes.

Example of final method

class Bike{  
  final void run(){System.out.println("running");}  
}  

class Honda extends Bike{  
   void run(){System.out.println("running safely with 100kmph");}  

   public static void main(String args[]){  
   Honda honda= new Honda();  
   honda.run();  
   }  
}  

Output:Compile Time Error
answer Dec 30, 2014 by Karthick.c
0 votes

Creating a final variable prohibits it from modification.
ex:--- final int x=10;
x=20; //INVALID

A final method cannot be overridden in the subclass of the class in which it has been defined.
ex:----- class Parent{
final int test(){}
}
class Child extends Parent{
int test(){} //INVALID
}

A final class cannot be subclassed.
ex:-- final class Base{
}
class Child extends Base{} // Invalid

The key to note is that a class having a final method still can be subclassed but it's final method can't be overridden in the child class.

answer Dec 29, 2014 by Prakash
Similar Questions
0 votes

By making private constructor, we can avoid instantiating class from anywhere outside and by making class final, no other class can extend it. Why is it necessary for Util class to have private constructor and final class ?

+2 votes
public class ServletClassName extends HttpServlet {
//declare instance variables for the servlet
int  instanceVar ; // not thread−safe
// overriding the Generic Servlet.init ( )
@Override
public void init ( ) throws ServletException {
// initialize instance variables, etc.
instanceVar = 0 ;
}
. . .
}

Per each page request, in the service() method (which eventually will be either doGet(), doPost(), or whatever),
you can manipulate instance variables.

Write a Servlet that counts the total number of visits (how many times it's being visited from all clients). The Servlet should display the following information

• Total Page Visits:
• Client Remote Address:
• Client Host Address: <client_fully_qualied_url__or__client_IP_address>

The last two can be obtained from the HttpServletRequest, Refer to the Java EE API

then how can re-do these using the Java API for HttpSession.

...