Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - How do I make an Interface?

Options
  • 06-01-2004 5:42pm
    #1
    Registered Users Posts: 841 ✭✭✭


    Howdy!

    Firstly, I'm not sure if an interface is what I need. :)

    I am trying to use methods from a class (HandyMethods.class) in my own package (simple methods that I use all the time in different apps). However I don't want to have to make an instance of that class and refer to it every time I want to call one of these methods (just too damn lazy!). I just want to call it by method name (as if the method exists in the class I'm workin on). I also don't want to use 'extends' becuse I already use 'extends JFrame or JDialog' in most of my classes and I don't think I can extend 2 other classes in one class? If I could use 'implements' I could list as many implemented classes as I wanted (ya know?).

    I have tried to make interfaces but I'm not entirely sure how they work. Java won't let me have anything in the body of methods in the interface when I try to compile it (what good is that?!).

    Thanks in advance. I hope I have described this adequately!


Comments

  • Registered Users Posts: 597 ✭✭✭bambam


    make the methods in HandyMethods static. so you can call HandyMethods.doXyz() from your class (don't forget to import it)


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    <edit: above post beat me:)> make the methods static, and use "dot notation" (ClassName.theMethod)


    you don't define a method body in the interface. you do that in ALL classes that use the interface.
    interface InterfaceExample
    {
    
    	//method returns a String, could return any object. could also be void
    	//No BODY defined. 
    	public String someMethod();
    }
    
    
    class Whatever implements InterfaceExample
    {//and therefore must have a someMethod() 
    
                   public String someMethod()
                   {
                            return "Hello wurrralld!";
                   }
    
    }
    


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    <edit: thanks Karoma.. just saw that.. obvviously not an interface I need then!>

    Thanks, That was quick! No joy tho :dunno:

    'static' works if I am calling HandyMethods a class but I want to make it an interface (or something like that :rolleyes: ) Example:
    package brian;
    
    public interface HandyMethods
    {
    	// This method prevents me from havin to type System.out.println all the time (told u I was lazy!)
    	public static void o(String message)
    	{
    		System.out.println(message);
    	}
    }
    
    ******// Class that uses HandyMethods
    
    import brian;
    
    class testInterface implements HandyMethods
    {
    	public testInterface()
    	{
    		o("Hello World");
    	}
    
    	public static void main(String[] args)
    	{
    		testInterface TI = new testInterface();
    	}
    }
    
    

    This doesn't work tho because it won't let me have anything in the method bodies of the interface. Anyone following this? :) Is there a way to do this or am I asking too much?


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    a little bit confustulated...but:
    interface InterfaceExample{
    
    	public String someMethod();
    }
    


    
    //define some methods with return types of
    //InterfaceExample references
    
    class SomeObject extends Object{
    
    	//public Object doSomeThing( ){
    	//returns InterfaceExample references
    	public InterfaceExample doSomeThing( ){
    
    		InterfaceExample ie;
    
    		return ie = new SomeObjectOne( );
    
    	}
    
    
    //class that agrees to implement the interface 
    //InterfaceExample in doing so it must define a method
    //someMethod() if it returns something then it must return
    class SomeObjectOne implements InterfaceExample{
    
    	public String doSomeMethod(){
    
    		return "Hello from SomeObjectOne";
    
    	}
    
    }
    


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Doh!:eek: Havn't a bog what you're on about here! Sorree. I will come back to this tho on one of those days where I understand things! (if you know what I mean, maybe tomorrow).
    Thanks Karoma - Imagine.. too lazy to type 'System.out.println'. tut tut tut :rolleyes:


  • Advertisement
  • Registered Users Posts: 1,865 ✭✭✭Syth


    Interfaces can't have method bodies. The best way to think about interfaces is to think of them as promises to write some methods. If interface MyInterface has a few metods, and if class MyObject implements it, then you (the programmer) are promising to write methods in MyObject with the same name as the methods in MyInterface.

    This way someone can look at the 'public class MyObject implements MyInterface' and can know that there are some methods are in the class MyObject.

    I sometimes need to use the same methods again and again, like file operations, so I just made a class called FileIO and wrote some static methods such as getFileContents(File f). Then when I want to get the contents of a file, I just call FileIO.getFileContents(f). Perhaps this is what you need?


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Thanks Syth,
    Pretty simple stuff, interfaces, when you put it that way..
    You have a good way of explaining things (a rare gift these days :))


Advertisement