The Artima Developer Community
Sponsored Link

Java Buzz Forum
Can we override static methods in java

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.
Can we override static methods in java Posted: Jun 28, 2015 5:03 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Can we override static methods in java
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
  • 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.



    Read: Can we override static methods in java

    Topic: 2 Ways to Read a Text File in Java 6 Previous Topic   Next Topic Topic: Eclipse's latest release train carries Docker, Java technologies

    Sponsored Links



    Google
      Web Artima.com   

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