![]() |
why can't a implment an abstract function as static?
I have an abstract function that I would like to implment in a subclass as static, since it simply returns a static variable. The compiler complains. I am wondering why? Anyone? |
Re: why can't a implment an abstract function as static?
"Ed Thompson" <ed4beckyNO_SPAM_2000@yahoo.com> wrote in message news:9mWTb.36078$YG.2896010@twister.southeast.rr.c om... > > I have an abstract function that I would like to implment in a subclass > as static, since it simply returns a static variable. The compiler > complains. I am wondering why? > > Anyone? Note: Java does not have functions, it has methods (and other things). All abstract and interface methods are implicitly non-static, abstract and public. An abstract method cannot be static, since abstract methods MUST be overridden by concrete implementations, and static methods cannot be overridden by definition. -- Tony Morris (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD) Software Engineer IBM Australia - Tivoli Security Software (2003 VTR1000F) |
Re: why can't a implment an abstract function as static?
Java wont let you change context/from to static when overriding
If a subclass re-declares a static method, it hides the base class method, it doesn't override it. Overriding only makes sense in terms of instance variables (think VMT) - static method implementations are determined by the compiler - hence hidden, not overridden - package a; class A { static void doSomething() { System.out.println("A.doSomething"); } public static void main(String[] args) { B b = new B(); b.doSomething(); A a = (A) b; a.doSomething(); } } class B extends A { static void doSomething() { System.out.println("B.doSomething"); } } the code above outputs : B.doSomething A.doSomething "Ed Thompson" <ed4beckyNO_SPAM_2000@yahoo.com> wrote in message news:9mWTb.36078$YG.2896010@twister.southeast.rr.c om... > > I have an abstract function that I would like to implment in a subclass > as static, since it simply returns a static variable. The compiler > complains. I am wondering why? > > Anyone? |
Re: why can't a implment an abstract function as static?
> Note: Java does not have functions, it has methods (and other things).
I know, I know, I just code in too many languages - job hazard (and joy) > An abstract method cannot be static, since abstract methods MUST be > overridden by concrete implementations, and static methods cannot be > overridden by definition. I was really referring to the subclass method being static: class a { abstract String getVar1(); } class b extends a { final static String var1 = "somelocalvalue"; static String getVar1() { return var1; } } why not? and why public only? |
Re: why can't a implment an abstract function as static?
"Ed Thompson" <ed4beckyNO_SPAM_2000@yahoo.com> wrote in message news:OWZTb.56073$5K1.3390850@twister.southeast.rr. com... > > Note: Java does not have functions, it has methods (and other things). > > > I know, I know, I just code in too many languages - job hazard (and joy) > > > An abstract method cannot be static, since abstract methods MUST be > > overridden by concrete implementations, and static methods cannot be > > overridden by definition. > > > I was really referring to the subclass method being static: > > class a { > abstract String getVar1(); > } > > class b extends a { > final static String var1 = "somelocalvalue"; > > static String getVar1() { return var1; } > } > > why not? and why public only? Without repeating what is already well documented for fear of introducing errors, I suggest you read the Java 2 Language Specification. Things may be clearer and hopefully will answer your "why" questions - perhaps deeper thought processes are necessary. -- Tony Morris (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD) Software Engineer IBM Australia - Tivoli Security Software (2003 VTR1000F) |
Re: why can't a implment an abstract function as static?
> compiler - hence hidden, not overridden -
I'm sorry for maybe a dumb question, but what's the difference between hidden and overridden? |
Re: why can't a implment an abstract function as static?
Flip wrote:
>>compiler - hence hidden, not overridden - > > I'm sorry for maybe a dumb question, but what's the difference between > hidden and overridden? Both involve name resolution / member access, but overriding implies polymorphic behavior whereas hiding implies non-polymorphic behavior. A hidden member is normally still accessible by use of an appropriately qualified name or other trick; an overridden member may not be directly accessible at all. In Java, overriding applies exclusively (and always) to non-static methods that have the same name and signature as methods in a superclass of their class. Any other name collision between subclasses and their superclasses results in hiding the superclass' relevant member. Accesses of hidden members can often be resolved at compile-time, but when there is a possibility of a method being overridden the method dispatch must be deferred to runtime. John Bollinger jobollin@idniana.edu |
Re: why can't a implment an abstract function as static?
Ed Thompson <ed4beckyNO_SPAM_2000@yahoo.com> writes:
> why not? Because "static" isn't part of the method signature. You cannot have both a static and non-static method with the same signature. >and why public only? Because methods in interfaces are defined to be public. The point of using interfaces sort of implies they should be. |
we cannot use static with abstract because abstract method can be overriddeen in subclass of that abstract class but static methods cannot be overridden even if u override there is no problem but u dont get output.becoz if u override the static method it will become local to that class thats all. thats why we should not use static along with abstract method .
but we can define static methods in abstract class. this is the main differnce between.. hope so this will give a good explanation.... |
protected abstract valid
Several of the responses are incorrect. While Java does not allow static abstract methods, Java does allow protected abstract methods. Java does not allow protected interface methods.
Please check the specification and/or test your answers before posting. This thread appears prominently in search results and I was shocked no one had corrected the assertions being made here. |
| All times are GMT. The time now is 05:41 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.