The Artima Developer Community
Sponsored Link

Java Buzz Forum
final static method in java example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
final static method in java example program Posted: Aug 21, 2016 11:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: final static method in java example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.  * @website: www.instanceofjava.com
  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. //  www.instanceofjava.com 

  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.  * @website: www.instanceofjava.com
  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. //  www.instanceofjava.com 

  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.  * @website: www.instanceofjava.com
  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. //  www.instanceofjava.com 

  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:






difference between static method and final method in java

Read: final static method in java example program

Topic: Good journalism looses money? Previous Topic   Next Topic Topic: The APM market is lively, growing 12% last year

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use