Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Interfaces

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interfaces

    I have looked up interfaces. Are interfaces similar to classes except they they only contain empty methods?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Interfaces

    Just as some applications require a user to intact with it, using a UI; some require interaction with other applications, systems and processes. This is done via an API. in java, an interface defines the entry point making it generic.

    This means, no matter who writes the entry point, as long as it implements the interface, it will always have the required signature. This allows multiple developers to work on integrations independently while keeping compatibility.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Interfaces

    WTF?

    Your entire post confused the cr@p out of me!
    Improving the world one idiot at a time!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Interfaces

    Another way to see interfaces is as a way to give a data type to a class. Java requires most arguments to methods to be of a certain data type. Interfaces allow one class to have many different data types and therefore can be passed as arguments to the methods that require that datatype.
    Most interfaces define a method that the class that implements the interface must define with code.
    Look at the ActionListener interface. It has one method you fill in for listening for button presses for example. The class that implements that interface can pass itself via the this variable to the addActionListener which requires a ActionListener for its argument.
    Last edited by Norm; December 7th, 2011 at 05:26 PM.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Interfaces

    More or less, a system which you are interacting with is expecting a square. If you have a number of different developers, say, Bob, John and Sam, who want to interact with this system, they will all write different code. Bob writes a triangle, John writes a hexagon and Sam writes a dodecahedron. The square from the system won't fit into any of these.

    An interface forces the developers to build the square. They can add extra functionality, but their entry point will be a square.

  6. #6
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    Ahh I get what your saying.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    So when I do "fileItem.addActionListener(new ActionListener(){});", I am adding a method named "actionPerformed" to the fileItem object?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Interfaces

    No. Your method of creating an anonymous class is incorrect.
    You need to add the method definition inside of the {}s

    Do a Search on the Forum for ActionListener for sample code.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    I know that , i couldnt be bothered to write it all out. So by writing:

    "fileItem.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
    code....
    }
    });",

  10. #10
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    ... i am adding the method actionPerformed to the fileItem object, and whenever an action occurs on this object the actionPerformed method is called?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Interfaces

    i am adding the method actionPerformed to the fileItem object,
    Not quite. You are adding an instance of an ActionListener object to the fileItem class's list of ActionListeners.
    whenever an action occurs on this object the actionPerformed method is called
    Yes

  12. #12
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    As an ActionListener is an interface, how can i add an instance of this? Because interfaces cant be instantiated can they.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Interfaces

    The compiler will create an anonymous (no name) class the way that you coded it in post#9.

  14. #14
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    Oh yeah

  15. #15
    Junior Member
    Join Date
    Jul 2018
    Location
    Mumbai, India
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Interfaces

    Interfaces are similar to class, known as reference type which provides polymorphism to a great extent, as they can only contain signature of abstract methods, which can be implemented by different classes as per the user requirement.

    They are the only way through which multiple inheritance cane be implemented in java.

Similar Threads

  1. About abstract, interfaces, and when i should use this.
    By Yoga Herawan in forum Object Oriented Programming
    Replies: 2
    Last Post: October 4th, 2011, 10:38 AM
  2. marker interfaces
    By anupam0021 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 06:53 PM
  3. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 08:51 PM
  4. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  5. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 07:11 AM