Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to override the static method

 
sachin santaji
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is possible to override the static method?
Yes than prove me?
No than prove me?

Program 1: By using subclass reference variable and object.

class A{
static void display(){
System.out.println("Display()-A");
}


}

class B extends A{
static void display(){
System.out.println("display()-B");
}

}
public class Display {

public static void main(String[] args) {
B b=new B();
b.display();

}

}

output:
display()-B


program 2: By using super class reference variable and subclass object

class A{
static void display(){
System.out.println("Display()-A");
}


}

class B extends A{
static void display(){
System.out.println("display()-B");
}

}
public class Display {

public static void main(String[] args) {
A b=new B();
b.display();



}

}

output:
display()-A



Do you know the internally how it works??
I understood the first program but not getting the second program flow?
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sachin santaji wrote:Is possible to override the static method?
Yes than prove me?
No than prove me?


No. Polymorphism of static methods is not supported.

sachin santaji wrote:
Program 1: By using subclass reference variable and object.



output:
display()-B


program 2: By using super class reference variable and subclass object


output:
display()-A

Do you know the internally how it works??
I understood the first program but not getting the second program flow?



Nothing fancy going on here. The compiler simply uses the reference type to determine which class to search for the static method. In the first case, the reference type is of the B class, and in the second case, the reference type is of the A class.

Henry
 
sachin santaji
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry:
You mean to say that according to Class reference type it changes the overridden calling method.
Thanks a lot Henry for solving my problem.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sachin santaji wrote:@Henry:
You mean to say that according to Class reference type it changes the overridden calling method.
Thanks a lot Henry for solving my problem.


There is *no* overriding taking place at all. This example is merely two different classes that happens to have a static method with the same name -- and as you already proven, both methods are accessible.

Henry
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually static methods are not overridden but they are re-defined (Conceptually) in the subclass which are called from the Reference type of an Object.
But the fun comes when you try to restrict the modifier or throw a checked Exception in the subclass static method. (Do check this)
so the static methods also follow some Overriding Rules.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote: . . . so the static methods also follow some Overriding Rules.
No, static methods do not follow overriding rules at all. There are howedver rules about inheritance, which they do follow.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sachin santaji wrote:Is possible to override the static method?

No, for all the reasons already mentioned.

Do you know the internally how it works??

No, and in general, neither the language nor the JVM specification are explicit about these sorts of things - for good reason.

What they do provide you with are rules; and those are what you should concentrate on.

However, unless you actually need this information for something (eg, the SCJP exam), the best advice I know of is to avoid the situation happening at all:
1. Don't define static methods unless you know they're needed (it's a common beginner's mistake to make everything static).
2. Don't define static methods with the same signature for more than one class in the same hierarchy.

HIH

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic