Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

About static method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i just want to know whether a static method can be overridden because in some of the mock exams it is given that it can and in some it cant. please clarify, its urgent.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static method can not be overridden...If you create another static method (With all the necessary requirments for overidding )in the sub class this is shadowing of the super calss method..not overridding...
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class StaticMethod1
{
public static void amethod()
{System.out.println("How R U");
}
}
public class StaticMethod2 extends StaticMethod1
{
public static void amethod()
super.amethod();//illegal

{System.out.println("I am ok");
}
public static void main(String[]args)
{
amethod();
}
}
In the example above the method amethod() is being shadowed , not overridden..as we can't call the original method with the call super.amethod() from the method of sub-class like we do for insatnce methods while overridding..
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
lets look at the example below to clarify whether static methods cannot be overridden.
class demo1
{
public static void x(){
System.out.println("super");
}
}
public class demo extends demo1
{
public static void x(){
System.out.println("this");
}
public static void main(String args[]){
demo1 x1=new demo();
// note reference of demo1 is created with an object of demo
x1.x();
// if overriding is done then base class method should be called
}
}

so what is the result???
the result is "super".
hope this helps.
regds.
Rahul.
 
Balaji M
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for Ur prompt reply. I may be bugging u for one more day. please dont mind.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic