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

Overriding problem

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Weed{
static final void growFast(){}
}
public class Thistle extends Weed{
void growFast(){}
}

Why does this code not compile if static methods cannot be overridden and can only be redefined?
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma ishu wrote:class Weed{
static final void growFast(){}
}
public class Thistle extends Weed{
void growFast(){}
}

Why does this code not compile if static methods cannot be overridden and can only be redefined?



You are using final keyword and expecting that it will allow you to use that identifier again ?
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enkita mody wrote:

sharma ishu wrote:class Weed{
static final void growFast(){}
}
public class Thistle extends Weed{
void growFast(){}
}

Why does this code not compile if static methods cannot be overridden and can only be redefined?



You are using final keyword and expecting that it will allow you to use that identifier again ?


But since static methods are not overridden so this is just a redefinition in another class. Isn't it? please elaborate.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma ishu wrote:

Enkita mody wrote:

sharma ishu wrote:class Weed{
static final void growFast(){}
}
public class Thistle extends Weed{
void growFast(){}
}

Why does this code not compile if static methods cannot be overridden and can only be redefined?



You are using final keyword and expecting that it will allow you to use that identifier again ?


But since static methods are not overridden so this is just a redefinition in another class. Isn't it? please elaborate.



I said remove final keyword first and then do whatever you want.final keyword neither allow overriding nor redefining.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma ishu wrote:
But since static methods are not overridden so this is just a redefinition in another class. Isn't it?


Static methods cannot be overridden but they can be inherited.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enkita mody wrote:

sharma ishu wrote:

Enkita mody wrote:

sharma ishu wrote:class Weed{
static final void growFast(){}
}
public class Thistle extends Weed{
void growFast(){}
}

Why does this code not compile if static methods cannot be overridden and can only be redefined?



You are using final keyword and expecting that it will allow you to use that identifier again ?


But since static methods are not overridden so this is just a redefinition in another class. Isn't it? please elaborate.



I said remove final keyword first and then do whatever you want.


I did as you said madam. But even after removing the final keyword, it is still showing compiler error.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I did as you said madam. But even after removing the final keyword, it is still showing compiler error.



You are not doing correct redefining i.e also called method hiding.

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enkita mody wrote:


I did as you said madam. But even after removing the final keyword, it is still showing compiler error.



You are not doing correct redefining i.e also called method hiding.


This is my question. If static methods can't be inherited, then why is this illegal?
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma ishu wrote:

Enkita mody wrote:


I did as you said madam. But even after removing the final keyword, it is still showing compiler error.



You are not doing correct redefining i.e also called method hiding.


This is my question. If static methods can't be inherited, then why is this illegal?



Look at the compile error i.e "overridden method is static"

Compiler expecting you to make overriding method static to implement method hiding.

This concept is called method hiding not overriding as you have misconception about it.
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sharma ishu wrote:
This is my question. If static methods can't be inherited, then why is this illegal?


An inherited static method can be hidden by a static method (not an instance method). An instance method cannot override a static method.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those legal eagles out there, this is specified in section 8.4.8.1 of the Java Language Specification.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.1

The exact quote is...

It is a compile-time error if an instance method overrides a static method.



This is one of the few parts of the JLS that seems to be very clear...

Henry
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic