Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   why can't a implment an abstract function as static? (http://www.velocityreviews.com/forums/t130999-why-cant-a-implment-an-abstract-function-as-static.html)

Ed Thompson 02-03-2004 11:43 PM

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?

Tony Morris 02-04-2004 12:57 AM

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)




Pat Ryan 02-04-2004 01:11 AM

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?




Ed Thompson 02-04-2004 03:46 AM

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?

Tony Morris 02-04-2004 04:21 AM

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)



Flip 02-04-2004 01:33 PM

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?



John C. Bollinger 02-04-2004 03:32 PM

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


Tor Iver Wilhelmsen 02-04-2004 05:32 PM

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.

siddhu 02-16-2011 06:45 AM

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....

lsamaha 05-24-2012 02:26 PM

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 07:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.