This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is it possible to override the static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to override the static method" Watch "Is it possible to override the static method" New topic
Author

Is it possible to override the static method

sachin santaji
Greenhorn

Joined: Mar 20, 2012
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sachin santaji
Greenhorn

Joined: Mar 20, 2012
Posts: 8
@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
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

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

Joined: Feb 06, 2012
Posts: 219

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.


OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
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

Joined: Mar 17, 2011
Posts: 7553
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to override the static method
 
Similar Threads
Assigning subclass obj to superclass ref
question on annonymous inner class
Hiding
Polymorphism : unexpected result ! why
static method can be overriden or not