• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

static method can be overriden or not

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai

i want to clarify one doubt regarding static method.

in K&B Scjp1.6 page no 161 at chapter 2 they specified static method can't be overriden but i executed this below code i got different thing



class A {
// public static void display(){

static void display(){
System.out.println ("Static method of A ");
}

}
class B extends A {

static void display(){

System.out.println ("Static method of B ");
}
}
class Static {
public static void main(String Args[]) {
A a=new A();
B b=new B();
a.display();
b.display();

}
}

output

Static method of A
Static method of B
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


if i write method in A with public specifier like public static void display(){

i am getting compile time error like

Static.java:10: display() in B cannot override display() in A; attempting to assign weaker access privileges; was public
static void display(){


normally the above error comes in overiding instance method trying to assign weaker pevilleges in Sub class if static method can not be able overridden means why the above message is comming
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the same rule applies for static methods as in case of overriding.
But at the time of calling static methods, they are called on the basis of the type of the reference variable.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused with the answer as much as with the question. Can someone clarify?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a difference when you try to override static methods.
They are actually hidden.
See this url for more details.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello every one.You could see the below code snippet which i have modified slightly
the code nippet which Sivagure natarajan have given in the forum

snippet 1



You could see that the line 1 and 2 is done using the class reference itself(Since the method A and B)
are static.This is of no wonder.

If this would have been a non static method illustrated by the following snippet

Snippet 2

you could see that the first line in snippet1 must have printed B since this reference points to the constructor of B but since

Static methods could not be overridden

It prints A.I hope this solves the problem and clarifies your doubts.If doubt still persists please feel free to use javaranch
 
sree visu
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i started replying the reply from paul was not there only when i posted i noticed it.His post gives a better explanation i think.Any way have a happy reading...
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva,
what i understand from your code is that when your adding public static void display() you are trying to overload static method display() in class A. So remember for overloading just changing the return type isnt sufficient. Thats why your getting the error.

class A {
// public static void display(){

static void display(){
System.out.println ("Static method of A ");
}

}
class B extends A {

static void display(){

System.out.println ("Static method of B ");
}
}
class Static {
public static void main(String Args[]) {
A a=new A();
B b=new B();
a.display();
b.display();

}
}
 
sree visu
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
friend vidhya suvarna ,


what i understand from your code is that when your adding public static void display() you are trying to overload static method display() in class A.



You could see clearly i did not point out that also that line which you pointed out is commented.You could see that in the code which i posted..
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods cannot be overridden. It can be hidden by the sub class methods.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods cannot be overridden but they can be redefined in the subclass.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Chander is referring to is shadowing? When a method isnt overriden (as its not inherited) but a method with the same name in an extending class is declared (or refined)?
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
member hunter9000 of the Sun Java Forums states:

The simple answer is that static methods are associated with the class itself, not with an instance of the class/QUOTE]

Thus the method which is called depends on the class you call it on. (unless you refer to it in a static context eg CLASS.method()

I think this helps the situation a bit
[ September 16, 2008: Message edited by: Stephen Davies ]

 
Ranch Hand
Posts: 362
  • 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.
i am also not getting why there is a compilation error?

can anyone give me clear explanation so that ranchers can clarify this doubt?
[ October 30, 2008: Message edited by: Ganeshkumar cheekati ]
 
Did you miss me? Did you miss this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic