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

Thread: Importance of interfaces

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Importance of interfaces

    Help me by explaining this. Its not only a pure abstract class , it has some other benefits. pls help me


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Importance of interfaces

    The specialty about interfaces is that you can have multiple interfaces be implemented by a single class. In standard Java inheritance you are only allowed to inherit from one super class (a fancy way of saying class you're inheriting stuff from). However, there are times you need to be able to treat that same object using two or more different perspectives, so interfaces were introduced to keep multiple inheritance out of Java. Other than that, I don't think there's any difference.
    public class A
    {}
     
    public class B
    {}
     
    // this is an example of multiple inheritance. This is illegal in Java
    public class C extends A,B
    {}
     
    public interface D
    {}
     
    public interface E
    {}
     
    // this has a similar effect to what's being done in class C, but is allowed in java.
    public class F implements D,E
    {}
     
    // this is also allowed in Java
    public class G extends A, implements D,E

    As far as I know, Java is the only language to have this strange interface deal (C++ allows multiple inheritance). However, I do only know Java and C++, so it's not really a comprehensive list at all
    Last edited by helloworld922; January 25th, 2010 at 05:19 PM.

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    jyothishey (January 25th, 2010), kitboy99 (February 9th, 2010)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Importance of interfaces

    Interfaces are important in many ways, from defining behavior across classes to facilitating maintenance, code reuse, and rearrangements in large projects. The reasoning why could probably fill up a book (and probably has), but suffice it to say the single inheritance rule in java that helloworld described limits a java programmer to write classes which inherit only from a single parent class. Interfaces allow one to write a behavior between classes that cannot be achieved by inheritance.

    Suppose you have a class Animal, and your program has utilized this base class to create all sorts of Animals using inheritance - for example Human, Lion, Bear, Hawk, Bat, Bird, etc...How can you easily specify behaviors - such as making a Bat and Bird fly; specifying that Lion, Bear, and Hawk are predators; specify which animals are nocturnal - if it cannot be accomplished by inheritance? Interfaces.

    Other more realistic examples can be found throughout the java API. This includes things like listener/callback type interfaces (if you are familiar with other languages which use function pointers). A real world example could be designing a program with the intention of keeping the user interface and algorithms separated - the connection between the two is provided via a interfaces. This design could allow the algorithms to be easier to adapt for different GUI's or programs - and vice versa - rather than being locked into the dependency of a certain class.
    Last edited by copeg; January 25th, 2010 at 07:45 PM.

  5. The Following User Says Thank You to copeg For This Useful Post:

    jyothishey (January 25th, 2010)

  6. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Importance of interfaces

    Hi,
    Thank you so much for giving this useful information. This really helps me

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Importance of interfaces

    I want more information regarding interfaces, help me

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Importance of interfaces

    An interesting point about interfaces, their declaration and implementation gets inherited along with the super class.

    public class Cat implements Predator
    {}
     
    public class Lion extends Cat  // by default, Lion also gets the interface Predator
    {}

    What else would you like to know about interfaces?

  9. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Importance of interfaces

    Useful information. I want to know, "when we need an interface actually in our application?"

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Importance of interfaces

    Creating your own interfaces is very useful in designing custom events, and if you have some common property that you want to force certain types of objects to have before they are allowed to be used by a method.

    The best way to learn about how to use interfaces is to learn more about polymorphism and experimenting with interfaces /inheritance yourself. Look through the Java API to see if you can see why they chose to use an interface at a particular time and while other times they chose to use an actual class (abstract or not).

    The deciding factors usually boils down to if you need that class/interface to have a super method that all inheriting objects need to have, but the implementation is the same (a class is a better choice here), if you need a non-static constant field in the super class (a class is a better choice here), or if you need that object to behave in multiple ways (an interface is a better choice here). There are tons of things to take into consideration, and a lot of times it doesn't matter which implementation you choose. In the words of one of my professors, "There is no one right answer, but there are a lot of wrong answers." All you need to do is find one that works, hopefully better than the bare minimum.

  11. #9
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Importance of interfaces

    Using interfaces you can control a set of classes only if these classes implement the corresponding interfaces.And your method can take a parameter of interface type,then the method will be able to receive kinds of variables only if they implement the interface.Thanks to interface,you can design some framwork, in the super framwork,the template will be fexed.All you need to do is implementing the concrete method of the interface.

    The memebers of the interface are public static final by default.And the member methods are public. Even thouth you don't explicitly qualified them as public. So if your interface is defined as follow:
       //interface 
      int count=0;
      String process();
    then it means:
      //interface
      public static final count=0;
      public String process();
    Notice that you'd not implement the interface like this:
      //implementation
     String process(){  // concrete operations}
    Because you are reducing the accessibility of the method.If this,the method become package access which is not allowable.
    Last edited by helloworld922; February 10th, 2010 at 03:41 PM. Reason: please use [code] tags

  12. #10
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 2 Posts

    Default Re: Importance of interfaces

    Using interface you can declare method in another interface without declaring new interface. But make sure that you have implement write interface otherwise it will give you error message. Using interface you can declare abstract methods in an abstract base class, because interface only gives description of method when you use this method in your program.