Automation Using Selenium Webdriver

Wednesday, 16 November 2016

try catch finally in java

try block:

  • The functionality of try keyword is to identify an exception object.
  • And catch that exception object and transfer the control along with the identified exception object to the catch block by suspending the execution of the try block.
  • All the statements which are proven to generate exceptions should be place in try block.
Syntax of try block:

try
{
//try block
//keep those statements which may
//throw run time exceptions
}

catch Block:
  •  The functionality of catch block is to receive the exception class object that has been send by the "try".
  • And catch that exception class object and assigns that exception class object to the reference of the corresponding exception class defined in the catch block.
  • And handle the exception that has been identified by "try".
  • "try"  block identifies an exception and catch block handles the identified exception.
Syntax:

catch(Exception e)
{
//catch block.
//one argument of type java.lang.Exception
//catches the exceptions thrown by try block
}

finally block:

  • finally blocks are the blocks which are going to get executed compulsorily irrespective of exceptions.
  • finally blocks are optional blocks.
Syntax:
finally
{
//This is the finally block.
}

Example Program:
  1. package com.instanceofjava;
  2. public class MyException {
  3.  
  4.  public static void main(String a[]){
  5.  
  6.   try{         
  7. int i = 10/0;             
  8.   System.out.println("This statement won't executed");      
  9.  } 
  10. catch(Exception ex){
  11.  
  12.   System.out.println("This block is executed immediately after an exception is thrown");
  13.  
  14.   }
  15.  finally{
  16. System.out.println("This block is always executed");}
  17.  }
  18.  
  19. }
Output:
  1. This block is executed immediately after an exception is thrown
  2. This block is always executed.

Java program to remove vowels from string java

  • java program to remove vowels from a string
  • To remove vowels from a string we can use predefined method of string  replaceAll()
  • By passing all vowels to the method replaceAll() with empty it will replaces all vowels with empty. 
  • Check below topic for more programs on string 
Program #1: Java example program to remove all vowels from a String



  1. package inheritanceInterviewPrograms;
  2. public class RemoveVowels {
  3.  
  4.     /**
  5.      * @http://corejavawithselenium.blogspot.in/
  6.      * @String interview programs asked in interviews
  7.      * @Remove vowels from a string in java
  8.      */
  9.  
  10.  public static void main(String[] args) {
  11.  
  12.         String str = "RemoveVowels";
  13.         String resustr = str.replaceAll("[aeiouAEIOU]", "");
  14.         System.out.println(resustr);
  15.  
  16.     }
  17.  
  18. }

 Output:


  1. RmvVwls





Static method vs final static method in java with example programs


Static methods are class level so there are not part of object.
So we can not override static methods but we can call super class static method using subclass name or instance also.
If we are trying to override static methods in sub class from super class then it will be method hiding not method overriding.



Means whenever we call the static method on super class will call super class static method and if
we are calling method using sub class it will call sub class method.
So it is clear that static methods are hidden not overridden and they are part of class means class
level not object level.
Now the question is can a method be static and final together?
For non static methods if we declare it as final then we are preventing that method from overriding
so it can not be overridden in sub class.
When we declare static method as final its prevents from method hiding.
When we declare final static method and override in sub class then compiler shows an error
Compile time error: Cannot override the final method from Super
Lets see an example program to understand this better.

Static methods in java

Program #1: Java example program to explain about static method in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * http://corejavawithselenium.blogspot.in/
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  static void method(){
  10.  
  11.  System.out.println("Super class method");
  12.  }

  13. }

  1. package inheritanceInterviewPrograms;

  2. //  http://corejavawithselenium.blogspot.in/

  3. public class Sub extends Super {
  4. static void method(){
  5.  
  6. System.out.println("Sub class method");

  7. }

  8. public static void main (String args[]) {
  9. Super.method();
  10. Sub.method();
  11.  
  12.  
  13. }
  14. }

Output:

  1. Super class method
  2. Sub class method

  • When we override static methods its not overriding it is method hiding and whenever we call method on class name it will call corresponding class method.
  • If we call methods using objects it will call same methods.

Program #2: Java example program to explain about calling super class static method using sub class in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * http://corejavawithselenium.blogspot.in/
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  static void method(){
  10.  
  11.  System.out.println("Super class method");
  12.  }

  13. }


  1. package inheritanceInterviewPrograms;

  2. // http://corejavawithselenium.blogspot.in/ 

  3. public class Sub extends Super {
  4. public static void main (String args[]) {
  5. Super.method();
  6. Sub.method();
  7.  
  8.  
  9. }
  10. }

Output:

  1. Super class method
  2. Super class method

  • We can call super class static methods using sub class object or sub class name also.
  • Now lets see what will happen in final static methods

Final static methods in java:

  • Can a method be static and final together in java?
  • When we declare a method as final we can not override that method in sub class.
  • In the same way when we declare a static method as final we can not hide it in sub class means we can not create same method in sub class. 
  • If we try to create same static method in sub class compiler will throw an error.
  • Lets see a java example program on final static methods in inheritance.

Program #3: Java example program to explain about final static method in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * http://corejavawithselenium.blogspot.in/
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  final static void method(){
  10.  
  11.  System.out.println("Super class method");
  12.  }

  13. }

  1. package inheritanceInterviewPrograms;

  2. //  http://corejavawithselenium.blogspot.in/ 

  3. public class Sub extends Super {
  4. static void method(){  // compiler time error:

  5. System.out.println("Sub class method");
  6.  
  7. }
  8. public static void main (String args[]) {
  9. Super.method();
  10. Sub.method();
  11.  
  12.  
  13. }
  14. }

Output: