• 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 method

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a non static method be overridden as static?

class Base{
public void print()
{
System.out.print("parent");
}
}

class Child extends Base{

static public void print(){
System.out.print("child");
}
class Test{

p.s.v.m(String []a){
Base b=new Base();
Base b2=new Child();
b2.print();

}

}
}
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can a non static method be overridden as static?



No you can't do this nor can you do the vice-versa. Neither can you override any Static method and if you do, that will be just hiding of your base method. Try doing this in any IDE like eclipse you will get compilation error-:
This static method cannot hide the instance method from Base
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class StaticMethod
{
static void method1()
{
System.out.println("super-class version");
}
}

class StaticMethodOverRiding extends StaticMethod
{
static void method1()
{
System.out.println("sub-class version");
}
public static void main(String args[])
{
StaticMethod s1 = new StaticMethod();
s1.method1(); // shud call super-class version and will call


StaticMethodOverRiding s2 = new StaticMethodOverRiding();
s2.method1(); // shud call sub-class version and will call

StaticMethod s3 = new StaticMethodOverRiding();
s3.method1(); // TRAP ::::shud call sub-class version as per overriding rules but will call superclass //verrion


}

}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just to conclude this thread, we also have to remember that static methods can't be overridden. They can be redefined in a subclass, but both are different things (redefining v/s overriding).

class Base() {
static void greet() {
system.out.println("Base ");
}
}

class Derived extends Base {
static void greet() {
system.out.println("Derived ");
}

public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Derived();

b1.greet();
b2.greet();
}
}

The output for the above would be:
Base Base

This is because, when the compiler sees b1.greet() or b2.greet(), it simply replaces it by Base.greet() ... as greet() is a static method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic