• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible to override the static method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic