File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implementing abstract/interface method as static 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 "Implementing abstract/interface method as static" Watch "Implementing abstract/interface method as static" New topic
Author

Implementing abstract/interface method as static

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi,
If an abstract class/interface is a contract for classes, and NOT objects, then how come it is NOT allowed to implement their methods as static?
Thanks.
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi,
An abstract class or interface defines a contract for subclasses. The subclasses are capable of creating objects (if those subclasses fully implement the abstract class or interface). Keep in mind that the objects created acording to the definition of those subclasses are also of same type as the abstract class or interface. Because the abstract superclass or interface is in the 'same tree'.
Therefore, an abstract class or interface is not directly a contract for objects but it is for subclasses. But fully implemented classes are a kind of contracts for objects, it defines how these class interact with their environment. One can conclude that abstract classes and interfaces are therefore indirect contracts for the objects which can be created by the subclasses of those abstract classes or interfaces.
By overriding methods to be static in the subclass you will break the indirect contract between the abstract class/interface and the subclass-object.
Hope this helps?
Rikko
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

Hello,
Here is what I found in the JLS about your question:
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

You can find it yourself with this link JLS - 8.4.6.2 Hiding (by Class methods)
[ July 26, 2003: Message edited by: Francois Roland ]

--
Brown belt on KnowledgeBlackBelt.com.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Hi Alton,
Here is a good reason to prevent declaring a method both as static and abstract ( so to prevent declaring methods in interfaces as static since they are implicitly declared as abstract and public).
Explanation from Dan's mock exam:

While the reference to an instance method is resolved at run-time based on the type of the referenced object; the reference to a static method is resolved at compile-time based on the type of the reference. Suppose the existence of an abstract superclass A and a concrete subclass C. Suppose that a reference, r, of type A refers to a concrete instance of type C. If r is used to invoke an instance method then the reference will resolve to the overriding implemenation in the subclass C. The same is not true for static methods. Since the reference to a static method is resolved at compile-time based on the type of the reference, the actual method referenced will be the static method in the superclass and not the subclass method. If a method could be declared both abstract and static, then the actual method referenced at run-time could be the abstract superclass method that provides no implementation

That's not the same for nested classes that may be declared both abstract and static, because the abstract nested class can be extended by another class.
So, following code:

will generate compilerat line 2 only.
Hope this help.
Link: http://www.danchisholm.net/apr18/topic/section1/nested1.html


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Oh sorry for my response, that did'nt answer to your question: this one was
- why not implementing an abstract method as static?
and not:
- why not declare a method both as static and abstract
I'm sorry for this misunderstanding...
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
(1) Interfaces do not contain implementation details.
Therefore, methods of interfaces must be overridden at run-time.
In Java, class methods are bound at compile-time (static binding)
and instance methods are bound at run-time (dynamic binding).
Therefore, methods of interfaces must be instance methods.
(2) Abstract classes may contain implementation details.
Abstract classes may declare static methods.
[ July 26, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
If an abstract class/interface is a contract for classes, and NOT objects

I tend to disagree. A class defines the contract between objects and clients of the objects.
then how come it is NOT allowed to implement their methods as static.

In Java, class methods are bound at compile-time (static binding). Instance methods are bound at run-time (dynamic binding).
We are told that static means a method is a class method, not an instance method. But I think static means static-binding (as opposed to dynamic binding), resolving a name to an implementation at compile-time (as opposed to run-time).
[ July 26, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi All,
Thanks for your replies.
I guess the point of my question is, when a class implements an abstract class or an interface, it is fulfilling a contract set forth by the abstract class/interface. So it is between the implementing class and the abstract class/interface. Whether the class decides that the implemented method is instance or static should be between the class and its objects.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
interface I { double m(); }
class A implements I { static double m() { return 0; } //hypothetical
class B implements I { double m() { return Math.random(); }
public void compute (I x) { x.m(); }
If some implementations are static and others are non-static, the name m in the method invocation x.m() could not be bound at compile-time.
Thus, class methods are no longer are bound at compile-time.
[ July 27, 2003: Message edited by: Marlene Miller ]
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Since m is not defined in the interface, then x.m() is an invalid method call. If, however, you had been able to define the static method m in the interface, then you would indeed require a dynamic binding to resolve the actual routine based on the object type of x.
Perhaps this is the issue - because static references are early bound, there is no way to allow them to be overridden. I'm still a bit confused, though. That can lead to what is to me counterintuitive behavior. For example:

Based on the above, what would you expect the results to be? Well, if I didn't know any better and I blindly applied my naive understanding of polymorphism, I'd expect it to say:
staticM1 in Static1
staticM1 in Static2
staticM1 in Static2
This is not the case, though. The actual result is:
staticM1 in Static1
staticM1 in Static2
staticM1 in Static1
Thus, two object references aliasing the exact same address may get different results to invocations of the same static method. This clearly shows that static references are early bound. I never knew this, and that might explain some odd occurences in one of my earlier design patterns.
The fact that static references are early bound is not something I particularly agree with. Then again, the fact that a single copy of a class (static) variable is shared among all subclasses has also bitten me in the past, so I need to think more carefully about the whole static issue. It is clear, now, that I can't think of class attributes (variables or methods) as simply "shared" versions of the corresponding instance attributes.
Joe
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implementing abstract/interface method as static
 
Similar Threads
Can a class be defined inside an interface?
Interface doubt
confusion with abstract
doubt regarding same method in interface and abstract class
Why cannot interface methods be static