• Exact answer is NO. We can not override static methods.
  • Before discussing this topic lets see what is static in java. 

Static methods in java:

  •  Static means class level if we declare any data or method as static then those data(variables) and methods belongs to that class at class level.
  • Static variables and static methods belongs to class level.
  • Static methods are not the part of objects state . Belongs to class
  • Without using object we can call these static methods.
 here the detailed explanation on static methods

Instance methods in java:


  • Instance methods will be called at run time.
  • Instance variables and instance methods are object level. variables values may change from one object to another where as static variable values are class level so if we access by using any object and changes that values will effect to all objects means its class level variable.
  •  Lets see about overriding in java.

Method overriding in java:


  •  Defining the super class method in sub class with same signature.
  • Even though in inheritance all properties of supers class can access in sub class we have an option of overriding super class methods in sub class known as method overriding.
  • So by this every-time sub-most object method will be called.

Instance methods - Method Overriding

  
  1. class SuperClassDemo{
  2.  
  3. public void instanceMethod(){
  4.  
  5. System.out.println("SuperClassDemo instanceMethodcalled");
  6.  
  7. }
  8.  
  9. }

  1. class SubClassDemo extends SuperClassDemo{
  2.  
  3. public void instanceMethod(){
  4.  
  5. System.out.println("SubClassDemo instanceMethod called");
  6.  
  7. }
  8. public static void main(String []args){
  9.   
  10.   SuperClassDemo superObj= new SuperClassDemo();
  11.   SuperClassDemo  superobj1= new  SubClassDem(); 
  12.   SubClassDemo subObj= new  SubClassDem(); 
  13.   // here no need to create object to call a static method please note that.

  14.   superObj.instanceMethod();
  15.   superObj1.instanceMethod();
  16.   subObj.instanceMethod();
  17.  
  18. }
  19. }

Output

  1. SuperClassDemo instanceMethodcalled
  2. SubClassDemo instanceMethodcalled
  3. SubClassDemo instanceMethodcalled

 Static methods - method overriding:

  • Lest an example program on static methods in method overriding concept and then discus about this clearly.
  1. class SuperClassDemo{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("SuperClassDemo staticMethod called");
  6.  
  7. }
  8.  
  9. }

  1. class SubClassDemo extends SuperClassDemo{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("SubClassDemo staticMethod called");
  6.  
  7. }
  8. public static void main(String []args){
  9.   
  10.   SuperClassDemo superObj= new SuperClassDemo();
  11.   SuperClassDemo  superobj1= new  SubClassDem(); 
  12.   SubClassDemo subObj= new  SubClassDem(); 
  13.   // here no need to create object to call a static method please note that.

  14.   superObj.staticMethod();
  15.   superObj1.staticMethod();
  16.   subObj.staticMethod();
  17.  
  18. }
  19. }



Output

  1. SuperClassDemo staticMethod called
  2. SuperClassDemo staticMethod called
  3. SubClassDemo staticMethod called

 Method Overriding - Method Hiding:

  • Method overriding : in method overriding we will define super class method in sub class with same signature and these methods will be called at run time based on the object.
  • Method Hiding: In method hiding even though super class methods are accesible in sub class they are not the part of sub class so the methods and these methods will be called based on the class.
  • In method Hiding if we define same static method in super class and sub class they are not same they are unique and distinct from the other.



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu