final is a keyword.it can be used for three posibilities.
they are
->we can assign the variable as final.
to declaring the variable as final to avoid the reusability of a variable.
now i display small program for this type.
class A
{
final int i=10;
a(int b)
{
i=b;
System.out.println(i);
}
}
public static void main(String args[])
{
A x=new A(20);
}
}
to execute this program .we have an error message.i.e.,the variable i can not be override.
->to use method have final keyword it can not be override.and also
->to use classes has final they can not be inhereted to sub classes.
//We Declare an abstract class as-
abstract class Car
{
}
//Example of an abstract method
abstract void hondaCity(); //no method body and abstract
//Example of Abstract class that has an abstract method
abstract class Car
{
abstract void run();
}
class HondaCity extends Car
{
void run()
{
System.out.println("runs smartly");
}
public static void main(String args[])
{
Car obj = new HondaCity();
obj.run();
}
}
//OUTPUT will be-
runs smartly
They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.
You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)
When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.
No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.Combining them would lead to an unusable class, so the compiler will not let this happen.
when overriding of a class or a method is necessary, they can be declared as abstract
if we declare a method as final then it can be changed.
They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.
You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)
Declare it final.
Declare the class as final. final class A{ ... }
When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.
Yes you can. Try this: public class TestMain { /** * @param args */ public static final void main(String[] args) { System.out.println("Inside final mail method..."); } } It will print "Inside final mail method..." in the console.
No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.Combining them would lead to an unusable class, so the compiler will not let this happen.
By using the final keyword in the class declaration statement. Ex: public final class Test {...}
You can declare a class as "final".
when overriding of a class or a method is necessary, they can be declared as abstract
a method declared final can not be overridden, and a class declared as final can not be extended by its sub class.