• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Methods

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read in Kathy Sierra 1.5 that static methods can't be overridden,but can be redefined....what does this mean....whats the difference between the two...plz help
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example might help you. Look at the following piece of code


Now try calling these methods as follows


The output will be


As you can see, the non-static method obeys the polymorphism rules. Though the reference is of Super type, the method is called on the Derived object. Overridding in work.

But on the other hand, the static method is called on the Super class, though it is redefined in the Derived class. This principle is called as hiding.

The static method in Derived class will be called only if you define the reference type as Derived -



The bottom line is,
The non-static methods in the parent class can be overridden by the child class. The static methods in the parent class can only be hidden or redefined by the child class, but cannot be overridden
[ March 24, 2006: Message edited by: Mani Ram ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, static methods can't be overridden.But, we can write the same static method signature in the sub class also(But this method is not inherited).
And one more thing is when you assign a sub class object to a superclass reference and call the overridden static method , which calls the super class method.(Which is not actually overridding).

Ex:

class Super{
static void show(){
Syste.out.println("in super");
}
}

class Sub{
static void show(){
System.out.println("in sub");
}
}

public class Test{
public static void main(String args[]){
Super sp=new Sub();
sp.show(); // prints "in super"
}
}


I hope this helps..

 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I failed to check the FAQ before answering the question
You can see a detailed explanation in the JavaRanch FAQ
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding applies only on instances.

Static methods and variables know nothing about instances. So we can hidden or redefine static methods but not override them.
 
nivi zal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks its clear now!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic