472,353 Members | 1,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 15293
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
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
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...
3
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...
6
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...
1
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,...
105
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...
13
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....
9
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...
25
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...
7
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.