why can't a implment an abstract function as static?

E

Ed Thompson

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?
 
T

Tony Morris

Ed Thompson said:
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)
 
P

Pat Ryan

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
 
E

Ed Thompson

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?
 
T

Tony Morris

Ed Thompson said:
I know, I know, I just code in too many languages - job hazard (and joy)



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)
 
F

Flip

compiler - hence hidden, not overridden -
I'm sorry for maybe a dumb question, but what's the difference between
hidden and overridden?
 
J

John C. Bollinger

Flip said:
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
(e-mail address removed)
 
T

Tor Iver Wilhelmsen

Ed Thompson said:

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.
 
Joined
Feb 16, 2011
Messages
1
Reaction score
0
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....
 
Joined
May 24, 2012
Messages
1
Reaction score
0
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top