Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Interface
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Sun Sep 19, 2004 2:19 pm   Post subject: Interface

What is the point of an interface?
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Sep 19, 2004 2:46 pm   Post subject: (No subject)

just as classes abstract objects.. interfaces abstract classes.. thus taking out a common element in different classes. it is also used as a substitue to multiple inheritance in a single inheritance environment (java).
Martin




PostPosted: Sun Sep 19, 2004 5:34 pm   Post subject: (No subject)

I'm still lost.

The program that we are making is an XML reader, and it says that we need an interface as well as a class. This strikes me as being fairly redundant...
rizzix




PostPosted: Sun Sep 19, 2004 7:23 pm   Post subject: (No subject)

yea if there;s only one class and one interface i'd say yep it is redundant
wtd




PostPosted: Mon Sep 20, 2004 1:25 am   Post subject: (No subject)

Interfaces sort of patch Java's type system.

With only java primitive types (int, double, boolean, the usual suspects), and classes, "things" in Java are classified purely by what they are.

The problem with this is that different "things" may be relatively unrelated, yet behave similarly enough to be used interchangeably in the correct circumstances.

Consider the following trivial example. I have a Tree class and a Dog class. Both have a "bark" method which returns a string. I want to have a static method in a Test class which takes an object and prints the result of bark.

Inheritance doesn't make any sense here, since Dogs and Trees aren't even remotely related (with the exception of the Dogtree).

I could just pass in an Object, but that just bypasses the type system and the safety it gives entirely. I could create two overloaded versions of the same method which eac take one of the two types (Dog and Tree) so far discussed, but that also poses problems. What if I want to pass in a Seal object. Seals bark, so they would be perfectly safe.

The good answer is that I create a Barker interface, which essetially creates a type designated not by what an object is, but rather what it can do.

code:
import java.lang.*;
import java.io.*;

interface Barker {
   public String bark();
}

class Dog implements Barker {
   public String bark() {
      return "Ruff!";
   }
}

class Tree implements Barker {
   public String bark() {
      return "Rough";
   }
}

class Seal implements Barker {
   public String bark() {
      return "Arfff!";
   }
}

public class Test {
   public static void main(String[] args) {
      Tree t = new Tree();
      Dog d = new Dog();
      Seal s = new Seal();

      printBark(t);
      printBark(d);
      printBark(s);
   }

   private static void printBark(Barker b) {
      System.out.println(b.bark());
   }
}


So, having the Barker interface allows us to use three very different objects in the same way when they share common behaviors or properties. Since this occurs quite frequently in the real world, this is a useful capability.

It also means we don't have to worry about runtime type errors as much as we would by just reducing everything to Object. If a class doesn't implement the Barker interface, then objects of that class simply can't be passed into the printBark method, and attempts to do so will be caught by the compiler.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: