• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Non static method overridding by static

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Shre Banerjee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the following link will help you a lot
So go ahead
Overriding Vs Hiding
 
Campbell Ritchie
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
All of the following truths are shameless lies. But what about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic