Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method

 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the differences between Method hiding and Method overriding?
Static methods cant be overriden,am i correct?
but...


class StaticSup
{
static void m1()
{
System.out.println("from StaticSup");
}
}
class StaticSub extends StaticSup
{
public static void main(String[] args)
{
System.out.println("Hello World!");
StaticSub obj=new StaticSub();
obj.m1();
obj.m2();


}
static void m1()
{
System.out.println("from StaticSub");

}
void m2()
{
System.out.println("from StaticSub m2");
super.m1();

}
}

expected o/p only it is displaynig
---------- Run ----------
Hello World!
from StaticSub
from StaticSub m2
from StaticSup
-----------------------------
How it is possible....
 
Ashley Waldrop
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StaticSub class is only redifining method "static void m1()" not overriding although it seems like that at first look

StaticSub obj=new StaticSub();
obj.m1();

Is same as
StaticSub.m1();


so should print "from StaticSub"
 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
If i replace
obj1.m1() and obj1.m2() with
StaticSub.m1() StaticSub.m2() resp.
It is giving compiletime error ,it is fine.
But what i am trying to ask you is that difference and
suppose if i want to test whether static methods are overriden or not what changes should i make in my code.
thanks a lot
 
Ashley Waldrop
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try something like this in previous code to get a clear picture whats going on

StaticSub obj=new StaticSub();
StaticSup sup = obj;
sup.m1();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic