Results 1 to 5 of 5
  1. #1
    gcampton Guest

    Default specified semantics for an interface.

    I'm having trouble not so much with interfaces but the way my book describes them, or more to the point. A particular point is made about interfaces.

    Interface semantics are not enforced.
    When you define a class that implements an interface, the compiler and run-time system will let you define the body of an interface method any way you want, provided you keep the method heading as it is given in the interface. However you should follow the specified semantics for an interface whenever you define a class that implements that interface; otherwise software written for that interface may not work for your class.
    I don't understand if interfaces are nothing more than simply declarations of methods, how can using it in whatever way you want make your code break?

    eg. if I create a interface called bully.
    Java Code:
    public interface Bully
    {
        public boolean bullied(Object victim);
        /**
         * This is pretty much just a random comment that
         * has no bearing on the interface, but is assumed
         * that the semantics contained within will be followed.
         */
    }
    From my point of view I can't see why I can't use this interface in anyway I like, eg, I could use this BULLY as like the comparable interface. I fail to see how it would wreck my code.
    So long as I implement a method called bullied that takes and object and returns a boolean, it doesn't really matter how I use it does it? (well it's bad practise but my point is the code shouldn't break should it?)

    Am I looking at this wrong? is there more to interfaces than is explained here?

    It does go on to talk about defined constants, and pitfalls of trying to overload etc. Demos on Cloneable, Serializable, and Comparable, but this is pretty much it.
    Last edited by gcampton; 12-21-2009 at 03:00 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at this interface:

    Java Code:
    public interface Worker {
       void initialize();
       void doWork();
    }
    By looking at the names of the methods you'll probably realize that you have to initialize() the implementation of the Worker but nowhere in the interface this is enforced; it is part of the semantics that you have to initialize() it first before you can make it doWork(). The documentation of the interface is supposedd to mention (and explain) the semantics of an interface.

    kind regards,

    Jos

  3. #3
    gcampton Guest

    Default

    By looking at the names of the methods you'll probably realize that you have to initialize() the implementation of the Worker but nowhere in the interface this is enforced; it is part of the semantics that you have to initialize() it first before you can make it doWork(). The documentation of the interface is supposedd to mention (and explain) the semantics of an interface.
    I get that, but if I create an abstract class implements Worker
    and use
    public void doWork()
    {
    setNumber(Math.random(666));
    }

    Would this not work? (ignoring the fact that's probably an incorrect call to Math) it is implied in the original comment that code may not work if you do not comply with the semantics, but at the same time is not enforced. So I fail to see how this can be true.

    another example implements comparable.
    public boolean compareTo(Object o)
    {
    return false;
    }

    this (while a stupid way to return false to something), would not cause the program to stop working.... I think.
    Last edited by gcampton; 12-21-2009 at 03:30 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcampton View Post
    I get that, but if I create an abstract class implements Worker
    and use
    public void doWork()
    {
    setNumber(Math.random(666));
    }

    Would this not work? (ignoring the fact that's probably an incorrect call to Math) it is implied in the original comment that code may not work if you do not comply with the semantics, but at the same time is not enforced. So I fail to see how this can be true.

    another example implements comparable.
    public boolean compareTo(Object o)
    {
    return false;
    }

    this (while a stupid way to return false to something), would not cause the program to stop working.... I think.
    Imagine that you only have an implementation of that (hypothetical) Worker interface and its documentation explicitly states: "thou shall call initialize() first before you attempt to do anything else with this object or else daemons will fly from your nose."

    Of course this is still no enforcement but you better call initialize() first before you attempt to do anything else. And of course there are counter examples for this Worker example interface but that is not the point: an interface itself can't force you how to treat any implementation of that interface but it can verbally or written warn you against any misuse.

    kind regards,

    Jos

  5. #5
    gcampton Guest

Similar Threads

  1. Is there a GUI interface for...
    By smith427 in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 07:17 AM
  2. what is the need of interface?
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 10:59 AM

Posting Permissions

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