- 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
- package inheritanceInterviewPrograms;
- /*
- * @website: www.instanceofjava.com
- * @category: Deference between staic and final static methods in java
- */
- public class Super {
- static void method(){
- System.out.println("Super class method");
- }
- }
- package inheritanceInterviewPrograms;
- // www.instanceofjava.com
- public class Sub extends Super {
- static void method(){
- System.out.println("Sub class method");
- }
- public static void main (String args[]) {
- Super.method();
- Sub.method();
- }
- }
Output:
- Super class method
- 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
- package inheritanceInterviewPrograms;
- /*
- * @website: www.instanceofjava.com
- * @category: Deference between staic and final static methods in java
- */
- public class Super {
- static void method(){
- System.out.println("Super class method");
- }
- }
- package inheritanceInterviewPrograms;
- // www.instanceofjava.com
- public class Sub extends Super {
- public static void main (String args[]) {
- Super.method();
- Sub.method();
- }
- }
Output:
- Super class method
- 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
- package inheritanceInterviewPrograms;
- /*
- * @website: www.instanceofjava.com
- * @category: Deference between staic and final static methods in java
- */
- public class Super {
- final static void method(){
- System.out.println("Super class method");
- }
- }
- package inheritanceInterviewPrograms;
- // www.instanceofjava.com
- public class Sub extends Super {
- static void method(){ // compiler time error:
- System.out.println("Sub class method");
- }
- public static void main (String args[]) {
- Super.method();
- Sub.method();
- }
- }
Output:
Crisp and clean.
ReplyDeleteThanks for your contribution, i am understanding java clearly with your elaborated answers.
ReplyDeleteThanks once again.
keep it up.very nice
ReplyDeletenice
ReplyDelete