• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding problem

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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?
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic