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 : " );      ...

Q) Retrieving data from database?

A)   ->to retrieve the data from database we need to submit SELECT statement from JDBC application. -> executeQuery() method of statement object is used for this purpose. This method has the fallowing ResultSet executeQuery(String sql) throws SQLException Ex: ResultSet rs = st.executeQuery(“SELECT * FROM ACCOUNT”); ->objectoriented representation of tables formate data is called ResultSet object. Ao the driver created one object i.e. ResultSet object. ->object orientation representation of a table of records returned from db is nothing but “ ResultSet ” object.  

Q) What is the purpose of JDBC?

A) -> Java application can do any task by making a method (function) call. ->java method calls are not understandable to database management system. They can understand only SQL statements. ->SQL statements can’t be directly used in a java application. Java compiler complains. ->therefore, we say that java environment and database environment are heterogeneous to each other. Purpose of JDBC: ->for any kind of java application to communicate with any kind of database (management system) in a standard manner, JDBC is used.