• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding static methods.

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your question, what do you mean by redefining?
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Devidas Menon Aniyath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

}
}
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic