interface


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: interface

  1. #1
    Join Date
    Jan 2008
    Posts
    1

    interface

    why we can't define the body of interface methods

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX

    That depends entirely on what you are trying to do.

    I can define a simple Lion interface
    Code:
    public interface Lion {
           boolean chaseZebra(Prey p);
           void eatZebra(Prey p);
    }
    What are you trying to do?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Methods CAN be defined in the definition of the Interface. However, the power of using Interfaces comes from the ability to create a definition of a method which has the same signature but different implementation among other classes. The way you get this is by not defining the method in the Interface itself, but inside of the definitions of the classes which implement that Interface.

  4. #4
    Join Date
    Jan 2008
    Posts
    8
    In its most common form, an interface is a group of related methods with empty bodies.

    Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

    To use an interface, you write a class that implements the interface. When an insatiable class implements an interface, it provides a method body for each of the methods declared in the interface.

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    So, we ask you, why can't you define interface methods?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you know the reference to any standard of the Java language which says you can't define a method in an interface? I've done it, so it is either within the standard or I got away with something I shouldn't have.

    My thought behind this is that there may be methods you want all members of this inheritance chain to implement, so why not define those methods in the Interface? There will be method implementations that you want to allow diversity among the children, so you just declare the method in the Interface and then define in all of the children who implement the Interface.

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by nspils
    I've done it, so it is either within the standard or I got away with something I shouldn't have.
    I seriously doubt if you got away with anything. Computers and computer programming languages are notoriously unforgiving when it comes to their limitations.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Replies: 6
    Last Post: 03-27-2002, 09:13 PM
  2. Problem with struct that implements interface
    By Michael Welch in forum .NET
    Replies: 5
    Last Post: 03-14-2002, 03:43 PM
  3. how to do explicit interface implementation ala C#
    By Aaron Anderson in forum .NET
    Replies: 2
    Last Post: 02-20-2001, 12:56 PM
  4. Replies: 0
    Last Post: 12-15-2000, 03:58 PM
  5. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 11:29 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center