wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding problem" Watch "Overriding problem" New topic
Author

Overriding problem

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Aug 06, 2012
Posts: 333

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 ?


OCA7
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Aug 06, 2012
Posts: 333

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

Joined: Jun 22, 2011
Posts: 83
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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Aug 06, 2012
Posts: 333


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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Aug 06, 2012
Posts: 333

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

Joined: Jun 22, 2011
Posts: 83
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
Sheriff

Joined: Sep 28, 2004
Posts: 18526
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Don't get me started about those stupid light bulbs.
 
subject: Overriding problem
 
Similar Threads
Overriding
top lavel static clas
can't run
static method
Mughal's Mock Exam-Main method declaration