dcsimg
Static Interface Problem
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   John_Senford
Posted On:   Thursday, September 18, 2003 03:26 AM

Summary ======= Define an interface which declares static methods. The essence of what I mean by this is: define a number of classes which contain common static methods. Details ======= What we are trying to achieve here is effectively an interface which garuantees us that the classes of a particular type (with a common parent class/interface) will all contain a specific method to which we have static access. Simple example ============== Here we have two classes that implement the Animal interface ( Dog and Cat ). Each class supplies a static method getType()    More>>

Summary

=======

Define an interface which declares static methods.


The essence of what I mean by this is: define a number of classes which contain common static methods.



Details

=======

What we are trying to achieve here is effectively an interface which garuantees us that the classes of a particular type (with a common parent class/interface) will all contain a specific method to which we have static access.



Simple example

==============

Here we have two classes that implement the Animal interface ( Dog and Cat ). Each class supplies a static method getType() which returns some non-instance-specific information (this method is static because the type is the same for all instances of the given class. E.g. ALL Dogs are of type 'Doggy').


			
interface Animal
{
...
}

class Dog implements Animal
{
public static String getType ()
{
return "Doggy";
}
...
}

class Cat implements Animal
{
public static String getType ()
{
return "Kitty";
}
...
}


The problem is: How can we garuantee a client application that all Animals will supply (in some way) this static method.


[In this particular example the methods differ only in the information they return, but the solution should allow the implementation of these methods to differ too].


Thanks for your thoughts.


JohnSenford
[@hotmail.com]

   <<Less

Re: Static Interface Problem

Posted By:   chris_moran  
Posted On:   Saturday, October 4, 2003 07:23 AM





Here's a simple way around your problem. Create an abstract class. Call it Animal: like so..



public abstract class Animal {
public final String getType() {
return this.getClass().getName();
}
<...some arbitrary abstract methods...>
}


This solves the problem but requires inheritence. Any class that subclasses Animal will return its own name when the getType() method is called on it. For example if you defined a class named Dog that extended Animal, the following:


Dog dog = new Dog();
String type = dog.getType();


The 'type' variable would have the String "Dog" in it. (Of course if the Dog class were in a package, the string would be prepended with the package path.


What you are trying to accomplish is not possible using just interfaces. Interfaces define a contract between class's implementation and its accessible methods. (in simple terms). Interfaces do not define implementation.

Re: Static Interface Problem

Posted By:   Anonymous  
Posted On:   Saturday, September 20, 2003 12:51 AM

You can not guarantee it - in Java, class methods aren't polymorphic; which method will be called is determined at compile time based on the type of the reference, not at runtime based on the type of the object.

I would advice you to simply make the methods non-static.

Re: Static Interface Problem

Posted By:   Amir_Pashazadeh  
Posted On:   Thursday, September 18, 2003 05:34 AM

Hi


Well this can't be done by the way Java compilers generate byte-code, when using a static member, Java compilers change create a code that is like ClassName.staticMember, and ClassName should be determinable at compile time, (that's why there is no overriding for static methods).


Creating this will cause static members overriding, and this doesn't seem so good.


I think some kind of redsign can solve the problem.


Amir Pashazadeh

Re: Static Interface Problem

Posted By:   Stephen_McConnell  
Posted On:   Thursday, September 18, 2003 05:01 AM

Problem number one is that Java does not permit "static" as a method modifier in an interface or abstract method.


It does allow you to do this with non-static methods, so you may have to instantiate your class to use a method like that.


If you define a method in an interface and a Class implements it, then the compiler will throw a compile error if the child class has not created an implementation of that interface method... The compiler will ensure that the method is available...


Hope this sort of helps.


Stephen McConnell

About | Sitemap | Contact