This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Is it possible to override the static method

 
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?
 
author
Posts: 23868
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23868
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Ranch Hand
Posts: 228
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.
 
Marshal
Posts: 68145
258
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!