Skip to main content

Q) Explain about “final” modifier?




A)

->”final” is a keyword in java.
->”final” modifier can be applied to variables, methods and classes as well.
->if final modifier is applied to variables, they become constants.
->final variables should be defined (should not be declared).
int a----variable declaration
int b=50; variable definition. I.e. at a time the variable declaration and initialization is done.
->once we define a final variable , we can’t assign a value to it programmatically(i.e. means we can’t change that value)
Ex:
Class A
{
final int a;          //error 
            final int b=30;   //ok
            void B()
            {
                  b=40;  //error will come , so we should not assign that final variable.
            }
}
->if the method is declared final, it can be inherited but can’t be overridden.
Ex:
class A
{
    final void x()
    {

     }
}

vlass B extends A
{
void y()
   {
      x();  //ok, final method can be inherited
   }
void x() // error. Can’t be overridden final method.
    {

    }
}
Explanation:
i.e. the method declared as final then that method can be inherited i.e. if that method declared in super class the subclass can be use that method but that method can’t be overriding in the sub class.
-> final class can’t be subclass. i.e. once a class is declared as final. It can’t be inherited.
Ex:
final class A
{
}
class B extends A  // it is error, can’t inherited from final class.
{
}
->generally the most specialize class is declared final to indicate that further specialization doesn’t add any value to the application and hence inheritance is prevented from that point (level).

                                                         
                                               
->some classes declared as final
Ex: Stringclass
That class declared as “final“ because that class should not inherited.
Class mystring extends string
{

}   // error. The string class declared as final.

Additional information:
->So that’s why it doesn’t inherited. To provide security some classes are declared as final.
For the more security purpose only, then that type of classes should not overridden with other class methods.
->sometimes the top most (i.e. root) class is declared as final. Because to prevent the substitution.
->the bottom most class (leaf class) in the hierarchy is declared as final.
->the most specialized class in the hierarchy declared as final.
->if a class is declared as final then that class can’t inherited.

Comments

Popular posts from this blog

Q) Explain repeated deletion of records.

A) //RepeatedDeletion.java import java.util.Scanner; import java.sql.*; class RepeatedDeletion {        public static void main(String[] args) {               Connection con= DriverManager. getConnection ( "jdbc:odbc:prince" , "scott" , "tiger" );               PreparedStatement ps=con.PreparedStatement( "DELETE FROM ACCOUNT WHERE ACCNO=?" );               Scanner s= new Scanner(System. in );               while ( true )               {                      System. out .println( "Enter accno : " );      ...

Introduction

1) What is not JDBC? 2) What is the purpose of JDBC? 3) What is ODBC? 4) What is JDBC? 5) What is JDBC architecture? 6) What is JDBC client? What are its responsibilities? 7) What is JDBC API?  8) What is driver manager? 9) What is JDBC driver? What is its role in java database connection? 10) What is database server?

Q) Explain about nested classes?

A) ->defining a class with in another class is known as nesting of the class. Ex: class A {        class B        {                      } //nested class, and inner class.because it is not static class. } //enclosing class or outer class. ->static class is declared in inside an another class then it does not become an inner class. Note: non-static nested class is known as “inner class”. In the above example class B is nested but not an inner class. Ex: class D {        static c        {                      } //nested class, but not an inner class , because of static class. } //outer class. ->we have three different kinds of inner classes. 1) Member inner class 2) Meth...