File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Non static method overridding by static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non static method overridding by static" Watch "Non static method overridding by static" New topic
Author

Non static method overridding by static

Shre Banerjee
Greenhorn

Joined: Apr 24, 2008
Posts: 3
I am not able to understand why non static java methods cannot be overridden by static methods

Thanks a lot
Shre
[ April 24, 2008: Message edited by: Shre Banerjee ]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
You don't need to understand why, you just need to know they can't.

It doesn't logically make any sense.

If something was a non-static method, logically it is acting on the values contained in the instance. To override the method with one that has no context would make no sense (and not work).



how would you override that with a static method?


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Shre Banerjee
Greenhorn

Joined: Apr 24, 2008
Posts: 3
I can understand that static cannot be overridden by non-static.
Example:

Class A
{
static void m1()
{
Sysytem.out.print(" m1 in A ");
}
}

Class B extends A
{
void m1()
{
Sysytem.out.print(" m1 in A ");
}
}

Class test
{
public static void main(String ar[])
{
A a = new B();
a.m1();// ----- (1)
}
}

Here (1) is resolved statically at compile time and m1() is never called for object b at run-time. I understand this.

Howver when I try the reverse, non-static overridden by static, why do I get compile time error. I would like a more detailed answer if someone has more knowledge as to how a.m1() is reolved in this case.

Class A
{
void m1()
{
Sysytem.out.print(" m1 in A ");
}
}

Class B extends A
{
static void m1()
{
Sysytem.out.print(" m1 in A ");
}
}

Class test
{
public static void main(String ar[])
{
A a = new B();
a.m1();// ----- (1)
}
}

Thanks in advance
Shre
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Again below is the answer to your question

Originally posted by Bill Shirley:
You don't need to understand why, you just need to know they can't.

It doesn't logically make any sense.

If something was a non-static method, logically it is acting on the values contained in the instance. To override the method with one that has no context would make no sense (and not work).



how would you override that with a static method?


Still Learing..
sree vankadara
Greenhorn

Joined: Apr 22, 2008
Posts: 10
I am getting compile time error in both cases.
I did not get how are you able to run it in the first case. (Static method overridden by non static method). Pls clarify
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
You are not overriding the static method at all. Let's get rid of the spelling errors in your quoted code (please use ctrl-C ctrl-V to quote code), and let's add an annotation

class B extends A
{
@Override
public void m1() . . .

and the compiler throws an error "method does not override or implement a method from a supertype".

So, you are not overriding the method at all, but overloading it with an instance method. Read what Bill Shirley said about "doesn't logically make any sense."

The reason you can't confuse static methods and instance methods is that instance methods may work differently for each instance of the class, and static methods work the same for every instance; in fact you should call static methods on the class, not on an instance.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
I think the following link will help you a lot
So go ahead
Overriding Vs Hiding


SCJP || SCWCD

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Originally posted by sachin verma:
I think the following link will help you a lot . . .
You are right, it is helpful. Thank you.
Shre Banerjee
Greenhorn

Joined: Apr 24, 2008
Posts: 3
Thanks a lot everyone.

Howver I am unable to understand that when we try to overridde instance method with static(not the reverse) as in above example, when we say
a.m1().... m1() is not static in A.
As compiler checks for reference type only and not object it should compile fine.Only when jvm tries to execute the meth at runtime should it realise that it is static in subclass B.


Is the question clear??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
Originally posted by Shre Banerjee:
Only when jvm tries to execute the meth at runtime should it realise that it is static in subclass B.
You mean only when JVM tries to execute the polymorphic method at runtime does it realise it is trying to modify instance variables from a static context? You are pretending the subclass extends the superclass, but when it comes to that method it behaves as NOT-A rather than IS-A.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non static method overridding by static