473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

another design question, abstract static methods...

so here is another general question about java...

why can't you declare an abstract static method.

i can envision the case (indeed i have experienced the case) where one
would want an abstracct superclass, with an abstract method such that all
subclasses that implement this method make that method static.

basically, the abstract class declares an abstract method that should be
static for all implementations (for example a factory method).

this isn't allowed in java, and in the language specification, there is no
reason given as to why. it is just simply stated in a single sentence
that an abstract method cannot be static. (or it turns out even the
implemented method).

can anyone explain why this isn't allowed? (i.e. give an example that
shows the flaws that can arise from such a situation).

thanks much!

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
3 15364
Polymorphism in Java is resolved by the type of the actual object (dynamic,
runtime) the method is called against instead of the class of the reference
(static, compile time). With static methods there is no object in play, only
a class so it can only ve resolved statically during compiletime.

What calling constructs (and behaviour) do you seek for?

Silvio Bierman
Jul 17 '05 #2
Murat Tasan wrote:
so here is another general question about java...

why can't you declare an abstract static method.

i can envision the case (indeed i have experienced the case) where one
would want an abstracct superclass, with an abstract method such that all
subclasses that implement this method make that method static.

basically, the abstract class declares an abstract method that should be
static for all implementations (for example a factory method).


I do not understand what you expect to accomplish this such a method.
How do you envision it being used? Perhaps if you provide a code
sample, I could understand.

From my point of view, I do not understand the point of a static
abstract method. Static method calls are determined at compile time,
not at run time, so it is not possible for the invocation to be
polymorphic. How would one invoke a static method so that one would
expect polymorphism in any case? If you have an instance of the class,
then the method might as well be non-static. If you are using
reflection, all that matters is that the method is defined in the
current class, the base class does not matter.

Furthermore, a abstract class is allowed to have abstract instance
method because it is not possible to instantiate an instance of the
class. But one may invoke static methods of an abstract class without
an instance. So there is no mechanism to ensure that an abstract static
method is not called.

Of course, many of the points I made above are really design decisions
of the Java language designers. So I am very interested in hearing how
you response to them to make the case for the other side.

Ray

Jul 17 '05 #3
guest
25 New Member
Here's an example where one would like to use an abstract static method -- or better yet, an interface with the static method! -- neither of which is allowed:

Expand|Select|Wrap|Line Numbers
  1. public abstract class GenericWidgetFactory {
  2.   public static abstract Widget createWidget(Object params);
  3.   }
  4.  
  5. public class BigWidgetFactory extends GenericWidgetFactory {
  6.   public static Widget createWidget(Object params) {
  7.     // code here to create big widgets
  8.     }
  9.   }
  10.  
  11. public class SmallWidgetFactory extends GenericWidgetFactory {
  12.   public static Widget createWidget(Object params) {
  13.     // code here to create small widgets
  14.     }
  15.   }
Expand|Select|Wrap|Line Numbers
  1. public interface GenericWidgetFactory {
  2.   public static Widget createWidget(Object params);
  3.   }
  4.  
  5. public class BigWidgetFactory implements GenericWidgetFactory {
  6.   public static Widget createWidget(Object params) {
  7.     // code here to create big widgets
  8.     }
  9.   }
  10.  
  11. public class SmallWidgetFactory implements GenericWidgetFactory {
  12.   public static Widget createWidget(Object params) {
  13.     // code here to create small widgets
  14.     }
  15.   }
This construct is very useful because it allows the compiler to enforce the syntactical structure of the inherited classes (Big and SmallWidgetFactory).

Any clever ideas around this without instantiating an object?
Thanks,
Jerry
Jul 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3118
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
3
2928
by: Microsoft | last post by:
Is there a way of implementing something like the Template Method design pattern, but only dealing with static methods ? First, I realized that I can't have abstract static methods in the base...
6
1806
by: Baris | last post by:
Given the C# code below, can anyone think of a better class design? What really gets my goat is that the code within derived classes D1 and D2 is identical and in my mind should be refactored into...
1
1425
by: Dave Clarke | last post by:
I'm looking for some feedback about an application I'm writing. In a nutshell a message is received, parsed, a mainframe transaction generated, and a response returned. I am intending to use a...
105
5229
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
13
1283
by: Don Miller | last post by:
Here's what I want to do... sorry for the lengthy description ;-) I have an inventory tracking system with items that I track stock levels on. Some items are counted with decimal precision...
9
3160
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
25
1504
by: gordon | last post by:
I aksed a few days ago about static methods and got some good answers that were really useful. Now I am not sure if about the use of static on members (variables) and classes. Can someone...
7
4454
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.