Thread: Java Question
View Single Post
Old 03-02-2005, 09:44 AM   #9 (permalink)
Iron_Cross
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Say I have an interface named MyInterface which contains two methods doSomething() and doSomethingElse() I would create the interface like this
Code:
public interface MyInterface{
    void doSomething();
    void doSomethingElse();
}
Then I'd use it like this
Code:
public class SomeClass implements MyInterface{
    public void doSomething(){
         // do some work
    }

    public void doSomethingElse(){
        // do some work here too
    }

    //
    // Anything else I want this class to have can go here
    //
}
Then to actually use that class I'd do something like
Code:
public class SomeOtherClass{
     public static void main(String[] args){
          // I can either use a direct instantiation like this
          SomeClass obj = new SomeClass();
          obj.doSomething();
          // When using direct instantiation I have direct access to
          // everything that class has to offer, all methods (non private)
          // all fields (non private) and so on and so forth

          // Or I can use indirect instantiaion
          MyInterface obj = new SomeClass();
          obj.doSomethingElse();
          // now I only have access to the interface methods
    }
}
Interfaces are good with multiple inheritence, abstraction, and reflection. Hope this clarifies it a bit. Oh, and you can use as many interfaces as you like, just separate them by commas.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline