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 6 of 6

Thread: interface

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default interface

    I am going to implement an interface but I don't want to override all of its methods, what should I do to not having some methods without body in my code?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: interface

    You just have to have an empty code body in your class.

    The hwol idea of an interface is to force you to implement the methods.

    Chris

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: interface

    The interfaces could be used for the inheritance between unrelated classes that are not part of the same hierarchy or anywhere in the hierarchy. Using the interface you can specify what to do a class but not how. A class can implement multiple interfaces. An interface can extend one or more interfaces using the keyword extends. All data members are public interface, static and final by default. An interface method can have only public, default modifiers.

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: interface

    You really need to override its methods... if dont want to have any code in its body, then just leave it blank.

  5. #5
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: interface

    Quote Originally Posted by jan_ed123 View Post
    You really need to override its methods
    unless the class itself is declared abstract.

  6. #6
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: interface

    I've been doing some reading on GUI's recently, and they involve interfaces when implementing the listener and the source of the Event. How is this relevant to your original question I hear you ask :p well in the afforementioned case you can bypass writing all the methods of the interface in your listener class if you use an 'adapter'. This is itself a class that houses all the methods of the relevant interface with blank bodies, you then have your listener extend said adapter - and over-ride the methods of the interface you do want to actually use.

    The same process can be used in more general terms:

     
    public interface MyInterface
    {
     
    void method1();
     
    void method2(); 
     
    void method3();
     
    }


     
    public class MyInterfaceAdapter implements MyInterface  
    {
     
    MyInterfaceAdapter() 
    {
    }
     
    void method1()
    {}
     
    void method2()
    {}
     
    void method3()
    {}
     
    }



     
    public class AnyRandomClass extends MyInterfaceAdapter
    {
     
    public AnyRandomClass()
    {}
     
    void method1 ()
    {
    System.out.println("inside method1")
    }
     
    // only uses one of the methods, and thus only defines one of the methods.
     
    }


    And presto, we have a class (AnyRandomClass) that implements an interface by means of inheritance. There fore allowing it to take advantage of polymorphism and what not. From this point on, you can keep on creating classes that extend the this adapter and again not have to implement all the methods of the interface that you don't need.
    Last edited by Bacon n' Logic; September 2nd, 2010 at 10:39 PM.

Similar Threads

  1. implementing interface from encrypted jar
    By mark111 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2010, 02:38 PM
  2. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
  3. what is the use of transient class and serializable interface?
    By chinni in forum Object Oriented Programming
    Replies: 3
    Last Post: October 28th, 2009, 05:48 PM
  4. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM
  5. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM

Tags for this Thread