aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding static methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding static methods." Watch "Overriding static methods." New topic
Author

Overriding static methods.

Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Hi All,

We cannot override static methods to non-static and non-static methods to static. Compilation fails if we try to do so. Then what is redefining?

Can anyone please explain with an example?

Thanks,
- Surya.


Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I don't understand your question, what do you mean by redefining?


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Here is the code which I got from K&B book which is compiling fine.



Here is my code which I wrote, NOT getting compilied.



I am not able to find out the difference.

- Surya.
[ October 03, 2006: Message edited by: Barry Gaunt ]
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi All,

We cannot override static methods to non-static and non-static methods to static. Compilation fails if we try to do so. Then what is redefining?

Can anyone please explain with an example?

Thanks,
- Surya.


Yes you are right, you can't override a static method with a non-static method and vice-versa is also not possible. You will get a compile time in an attempt of doing so.

By saying redefining you probably means hiding.
We don't override static methods we hide them.



HTH,


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Devidas Menon Aniyath
Greenhorn

Joined: Apr 28, 2006
Posts: 12
Hai Sai Surya,
I think the below code helps

class Test
{ static void show()
{
System.out.println("Static method in Test");

}
}

public class TestDemo extends Test
{
static void show()
{
System.out.println("This is not Overriding.It's redefining");
}

public static void main(String[] args)
{
TestDemo td=new TestDemo();
Test t=new Test();
t=td;
t.show();

}
}


Anything acquired without effort, and without cost is generally unappreciated, often discredited<br />-:Napoleon Hill
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Originally posted by Sai Surya:
Here is the code which I got from K&B book which is compiling fine.

<CODE>
class Animal {
static void doStuff() {
}
}

class Dog extends Animal {
void dostuff() {
}

public static void main(String [] args) {
}
}
</CODE>

Here is my code which I wrote, NOT getting compilied.

<CODE>
class Parent {
static void aMethod() {
}
}

class OverrideTest extends Parent {

void aMethod() {
}

public static void main(String[] args) {
}
}
</CODE>

I am not able to find out the difference.

- Surya.



I got it. The doStuff() in class Dog has letter "s" small, now I am clear.

Thanks,
Surya.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
My question about this is: was the second 'dostuff()' meant to be written with a small 's'? Because technically those two methods wouldn't have anything to do with eachother, since Java is case-sensitive. I'm almost sure this is an errata, except I couldn't find it in the actual errata's.

So, I think Sai's example is not showing 'redefinition' of a method like they explain in the book. That example is showing
static void aMethod()
void aMethod()
and compilation fails because you changed a static method to a non-static method.

In the book example as it is, compilation succeeds because there are two different methods (notice the case), being
static void doStuff()
static void dostuff()
.

The actual example should be
static void doStuff()
static void doStuff()
showing exact the same bodies.

I would like confirmation on this though ...
[ October 02, 2006: Message edited by: Dylan Honorez ]

SCJP - SCWCD
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by Dylan Honorez:
That example is showing
static void aMethod()
void aMethod()
and compilation fails because you changed a static method to a non-static method.


it failed because it's trying to "override" the method (in my IntellJ IDEA at least).

agreed with second one. should be on errata list.


SCJP 5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hai Sai Surya,

Your question

"We cannot override static methods to non-static and non-static methods to static."

It's perfectly legal to override or overload a static method. It's quite similar to doing for non-static methods. The only difference is that when you call a overriden static method without referring to it's class or without referring to any object, the method in the parent class gets called.
Consider the following code,




I hope the explanation is clear. Any mistakes or any improvements or any furthur explanation for the above code from other ranchers is welcomed.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Reffering to my above code. I made a mistake by saying that static methods can be overriden.

No static methods can be overriden. They can be redefined. In the above code, if you try this you will know that static methods can't be overriden.

Main m = new Main1();
m.print();

In the code snippet, m refers to a subclass object which as per the rules of overriding shud print child. But it prints parent which means it is not overriding. so kindly apologize my mistake.

Regards,
Jothi Shankar Kumar. S
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
So let's sum up: static methods can't be overridden in the meaning that when you have a method with the same name in a child class, polymorphism won't occur like it does with normal instance methods. That's why we say, you redefine a static method, you don't override it. Keep in mind that this new method you redefine your original method with has to be static too, else compilation will fail.
[ October 03, 2006: Message edited by: Dylan Honorez ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi Dylan,

Thanks for summing it all up. So static methods can't be overriden nor be overloaded. Anyone for corrections on this?

Regards,
Jothi Shankar Kumar. S
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Dylan,

Thanks for summing it all up. So static methods can't be overriden nor be overloaded. Anyone for corrections on this?

Regards,
Jothi Shankar Kumar. S


Static methods can be overloaded.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Yes, static methods can not be overridden, if they do then it would all violate the meaning of them being "static". Static means one copy, single copy and the only copy, it is same for parent, it is same for child, it is same for parent object, it is same for child object.

There is no question of static methods being overridden, as the concept of overloading comes with method signature and the signature of a method doesn't include any modifiers.

Well, I wish there was only one way to access static methods through "class-name" that is it. Anybody, any idea why do they still permit the via-object-access for static methods??


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi Barry,

Static methods can be overloaded??? Can you tell me how with an example. I have posted a similar topic

http://www.coderanch.com/t/259589/java-programmer-SCJP/certification/Static-overloading

Please let me know.

Regards,
Jothi Shankar Kumar. S
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Yes, static methods can not be overridden, if they do then it would all violate the meaning of them being "static". Static means one copy, single copy and the only copy, it is same for parent, it is same for child, it is same for parent object, it is same for child object.

There is no question of static methods being overloaded, as the concept of overloading comes with method signature and the signature of a method doesn't include any modifiers.

Well, I wish there was only one way to access static methods through "class-name" that is it. Anybody, any idea why do they still permit the via-object-access for static methods??
[ October 03, 2006: Message edited by: Akhilesh Trivedi ]
Devidas Menon Aniyath
Greenhorn

Joined: Apr 28, 2006
Posts: 12
Hi
Here I have try to explain it with an example...Hope it will help you

package test4;

class Base {

public static void display() {
System.out.println("Display method in base class");
}
}


public class Derived extends Base {

public static void main(String[] args) {
display("overloading");
}
public static void display(String displayString) {
System.out.println("Overloaded Display Method in Derived Class "+displayString);
}
}
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi Akhilesh Trivedi,

Your statement,

There is no question of static methods being overloaded, as the concept of overloading comes with method signature and the signature of a method doesn't include any modifiers.

I can't justify it. Please explain with an example.

Regards,
Jothi Shankar Kumar. S
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Jothi, where am i not clear to you buddy? do you get what is the difference between method and method signature?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here is a class with three overloaded static methods:


Here is a subclass of A with a fourth and fifth overloaded versions of the method:


For the three calls to m1 in main the compiler must make a decision of which of the overloaded m1 methods it must call. It can call an m1 defined in its own class or it can call one that its class has inherited. It must also decide which method takes the actual arguments provided.
[ October 03, 2006: Message edited by: Barry Gaunt ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Thanks for all your reply. But with respect to the above code what will the compiler do? Which method will it call and why?

Regards,
Jothi Shankar Kumar. S
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Jothi Shankar Kumar Sankararaj:
Thanks for all your reply. But with respect to the above code what will the compiler do? Which method will it call and why?

Regards,
Jothi Shankar Kumar. S


The code compiles and runs so try it yourself. Perhaps you should add some print statements in the method bodies, though.
[ October 04, 2006: Message edited by: Barry Gaunt ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi Barry,

I just tried it and I got to understand it. Thanks for all your help. It was great.

Regards,
Jothi Shankar Kumar. S
 
Consider Paul's rocket mass heater.
 
subject: Overriding static methods.