• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static methods  RSS feed

 
Venkat Ramanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain why Static methods cannot be overriden but they can be overloaded?
Thanks
Venky
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi venkat,
Static method can be overriden, but by only static methods.
Static methods can not be overriden by non-static methods.
eg.
You can override
public static void main(String sha[]) also
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkat,
Static method cannot be overridden in any way.
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods.
Class method cannot be overridden. The example:
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Venkat"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "shashank"; }
}
class Test {
public static void main(String[] args) {

Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());

}
}
produces the output:
Goodnight, Shashank.
because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.
Actually we have to think about class method ie static method
and instance method ie. non-static method. Class method can
not be overriden except hiding in other hand instance method
should be overridden.
- Golam Newaz
------------------
 
Venkat Ramanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnkx
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Golam,

For making me clear that static mtds can not be overridden, we can hide them.
Thanx!
Shashank
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Golam and Venkat!
This also shows another face of polymorphic behavior for static methods : "For static methods - there is no polymorphism".
Consider following
code from Golam:
[CODE]
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Venkat"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "shashank"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
{/CODE]
Had greeting () been a normal method,
out put would have been : Hellow Shashank .

Shrinivas

 
Lokesh Mahajan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Golem Nawaz,
I didn't get the concept of static method not being overridden and you said that it is hiding or something of that sort.
Lets assume that they are not overridden, then according to you the following piece of code should work. However it does not
Let me illustrate the point with an example
class X {
public final static void show() {
System.out.println("Hello Java");
}
}
public class Y extends X {
public static void show() {
System.out.println("Hello C++");
}
}
Now suppose if static methods are not overridden then making it final in the base class shouldn't affect the working of the derived class. However when I try to compile the program I get the following error.
The method void show() declared in class Y cannot override the final method of the same signature declared in class X. Final methods cannot be overridden.
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lokesh,
You put another one keyword into your program ie. final.
So we have to think two things. One static and one is final.
Look i changed your program a little in line no.2 just
dropped out static i got the following output.
Output:
The static method java.lang.String show() declared in class
Sub cannot hide the instance method of the same signature
declared in class Super. It is illegal to hide an instance
method.
The method void show() declared in class Y cannot override the final method of the same signature declared in class X. Final methods cannot be overridden.

1 class X {
2 public final void show() {
3 System.out.println("Hello Java");
4 }
5 }
6
7 public class Y extends X {
8 public static void show() {
9 System.out.println("Hello C++");
10 }
11 }
So it has been proved that static method related with hidden not override.
- Golam Newaz
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!