Automation Using Selenium Webdriver

Wednesday, 16 November 2016

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:


1 comment:

  1. Your blog is great. Practically, it was awesome and useful for us. Keep sharing. Selenium Training in Pune

    ReplyDelete