Results 1 to 4 of 4
  1. #1
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default What is the point of an interface?

    Hello,

    I was reading about interfaces and was confused as to why they existed. For example.

    Java Code:
    public interface Bike
    {
    
    
    public Boolean isBike()
    {
    }
    
    
    }

    and I make a class the utilizes that interface

    Java Code:
    public class Giant implements Bike
    {
    
    public Boolean isBike()
    {
    //Has to have an actual body to it
    
    }  
    }

    It just seems a little redundant because you can't write any part of the method in the interface itself, it has to be blank. It would make more sense to for the programmer to just forgo using the interface and to write the methods anyway. It doesn't seem to make any difference. I am just wondering if I am missing something, and would like to learn more. Thanks for your time.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What is the point of an interface?

    Java is a strongly typed language. Many methods require an argument of a certain type. An interface is a way to give a type to a class. Along with the type is a requirement that the class have all the methods defined in the interface.
    For examples look at the add listener methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What is the point of an interface?

    Does your example compile? I ask because the isBike() declaration in the interface, as you have written it, does include a method body: an empty one. Moreover isBike() doesn't seem a particularly useful method of a Bike type (whether class or interface). "All Bikes are bikes" says Aristotle. Only one implementation seems possible: return true;

    -----

    Roughly speaking, classes represent a type of thing, while interfaces represent an ability - or set of abilities - that a thing (of whatever type) might posses.

    For instance a Person class might represent a type of thing (a human being) while a Singer interface might include such disparate entities as Maria Callas (an instance of the Person class) and Tweety Bird (an instance of FictionalPasseriformes). The point is that the abilities that make something implement an interface are not enough to precisely define it as a certain sort of thing. Specifically, both Maria and Tweety sing(), but they do so in very different ways: the Person sing() method is said to be implemented quite differently than the FictionalPasseriformes sing() method.

    If you feel that there is something a little dodgy is the forgoing description, you are probably right. Who is to say whether some set of abilities is enough to define a certain type of thing, or whether they are merely abilities held in common by various different sorts of thing? The distinction is somewhat fuzzy - so much so that the What is an Interface? page in Oracle's Tutorial gives a Bicycle as an example of an interface. I would have thought that a Bicycle is a sort of thing. And that the abilities they list there (changeCadence(), changeGear() etc) apply as much to instances of MotorCar as to instances of a Bicycle class. It seems to be a case of YMMV.

    It would make more sense to for the programmer to just forgo using the interface and to write the methods anyway.
    The programmer must write the implementation of the methods anyway. So it seems your question is well asked. Given that you have to implement the method, why state in a one type (the interface that you are later going to implement it), and then honour your promise in quite another type (the implementing class)?

    Suppose I were writing a piece of music: Quodlibet for Soprano, Fictional Passeriformes, and Others. I would have to implement a sing() method in the Soprano class (or possibly its superclass, Person), and also in the FictionalPasseriformes class. I am not even sure at this point what type of thing the others might be only that they have a sing() method. One attempt at the Quodlibet class might look like:

    Java Code:
    class Quodlibet {
        public void perform(???[] singers) {
            for(??? singer :singers) {
                singer.sing();
            }
        }
    }
    (Not a very good attempt because the singers sing one after another, rather than together...)

    As you can see I am stuck on how to declare the perform() method. I cannot say "public void perform(Person[] singers){..." because of Tweety. I cannot say "public void perform(FictionalPasseriformes[] singers){..." because of Maria. And I haven't even written the classes to which the others belong yet.

    What I ought to do in this case is invent a new type of thing: something that represents the ability to sing that disparate classes have in common. A promise that any classes implementing this new type will have a sing() method. An interface:

    Java Code:
    interface Singer {
        void sing();
    }
    Then I make sure that Soprano, FictionalPasseriformes and any class I write later implement Singer.

    All that is done so that Quodlibet can be written in a way that precisely expresses my intention:


    Java Code:
    class Quodlibet {
        public void perform(Singer[] singers) {
            for(Singer singer :singers) {
                singer.sing();
            }
        }
    }
    Not sopranos, cartoon birds, or any other specific type yet to be written, but an array of Singers-of-whatever-type can be used by the perform() method.
    Last edited by pbrockway2; 03-25-2012 at 02:16 AM.

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: What is the point of an interface?

    That was an amazing explanation. Thanks both of you. I threw together that interface as a rough example, I realize it's useless.

Similar Threads

  1. What's the point of try {} ?
    By Josep_16 in forum Java Applets
    Replies: 2
    Last Post: 08-05-2011, 01:47 PM
  2. Can someone point me to an example?
    By smhjr in forum New To Java
    Replies: 2
    Last Post: 08-27-2009, 11:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •