Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interface with static functions?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I can see that functions can't be static and abstract, which makes sense for polymophism, but I'd like to create an interface (or something) that will force implementing classes to implement some static functions.

Is that possible somehow?
 
Not to my knowledge. Since you can't derive from statics, and since every method declaration in an interface is required, by definition, to be overridden, I wouldn't expect it to work anyway.

Tim
 
Yeah, that's what I figured... :-( It would be a nice feature to have though.
 
@timw: What do you mean by "you can't derive from statics"?

I would say:
Static Methods are called without a concrete Object, but for a class.
An Interface Fooable with a static method foo () would be called by Fooable.foo (), but without Object, which concrete method should be called?

don't visit my homepage:
 
I didn't quite understand that?

I'll give you a pseudo-code example of what I was looking for (either interface or abstract class)...
Code:
abstract class Base
{
   public static boolean IsValueCorrect( int  value )
   {
      value = preSetup( value ); // Call static function that must be defined in derived class.
      // Do some common work here.
      value = postSetup( value ); // Call static function that must be defined in derived class.
      return (value == 0);
   }

   protected abstract static int preSetup( int  value );
   protected abstract static int postSetup( int  value );
 ...
}

public class Derived1  extends Base
{
   protected static int preSetup( int  value )
   {
      // No pre-setup needed for Derived1.
      return value;
   }

   protected static int postSetup( int  value )
   {
      // No post-setup needed for Derived1.
      return value;
   }
 ...
}

public class Derived2  extends Base
{
   protected static int preSetup( int  value )
   {
      if ( value < 0 )
      {
         throw new InvalidArgumentException( "Value cannot be negative!" );
      }

      return value;
   }

   protected static int postSetup( int  value )
   {
      if ( (value == 2) || (value == 13) )
      {
         value = 0;
      }

      return value;
   }
 ...
}
 
Stefan, have you tried putting a static method in an interface? Can't be done I think.

But I reread my post and realised that I was talking nonsense when I said 'derive from statics', since it is a class which is derived, not methods.

Tim
 
I've had the desire to do this before in C++, but I don't remember why I thought it might be necessary.

In the pseudo code above, you can call a regular (virtual) non-static method that can either do the work or call another static function from that class. The negative is that you need two methods per class just so that you can put the code inside a static function.
 
uolj said:
you can call a regular (virtual) non-static method that can either do the work or call another static function from that class.
But then I'd need an object instance to call the virtual function. Since the work the function is doing has nothing to do with the state of the object, ideally it should be static.

I wonder if there is a way to do this using generic static functions? If there is, I just can't see it.
 
Ok, I overlooked the fact that IsValueCorrect is static.

This was probably the situation that had me stumped earlier. I guess the solution would be to just be ok with the fact that the functions aren't static.
 
@timw: Well - yes.
A call to Base.isValueCorrect (7); is done without a concrete (derived) Object of Type Base (Derived1, Derived2) - so which method should be called?

I can see some value in calling
Code:
 if (Derived1.isValueCorrect (42) && Derived2.isValueCorrect (-7))
// ...
- yes, and perhaps you know in advance, that there will never be an implementation, where it would be useful to have a non-static implementation of preSetup or postSetup, while your example leads me immeadiately to such a class:
Code:
 class Derived3 extends Base
{
	private int lower, int upper;

	public Derived (int lower, int upper)
	{
		this.lower = lower;
		this.upper = upper;
	}
	
	public int postSetup (int value)
	{
		if ((value > lower) && (value <= upper))
		// ...
You can make isValueCorrect a static method by changing the classdesign:
Code:
interface PreSetup	{ int preSetup (int value);}
interface PostSetup	{ int postSetup (int value);}
interface PrePostSetup extends PreSetup, PostSetup {}

final class Base
{
	public static boolean isValueCorrect (PrePostSetup pps, int value)
	{
		value = pps.preSetup (value);
		// Do some common work here.
		value = pps.postSetup (value);
		return (value == 0);
	}
}

final class Derived1 implements PrePostSetup
{
	public int preSetup (int value)
	{
		return something;
	}

	public int postSetup (int value)
	{
		return somethingElse;
	}
}

public class BddTest
{
	public static void main (String args [])
	{
		PrePostSetup d1 = new Derived1 ();
		PrePostSetup d2 = new Derived2 ();
		
		System.out.println (Base.isValueCorrect (d1, 7));
		System.out.println (Base.isValueCorrect (d2, 7));
		
		System.out.println (Base.isValueCorrect (d1, -42));
		System.out.println (Base.isValueCorrect (d2, -42));		
	}
}
You don't need multiple objects to call Derived1.preSetup with differnt values.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top