This discussion is archived
7 Replies Latest reply: Dec 13, 2012 1:09 AM by TPD-Opitz-Consulting-com RSS

Interface

674072 Newbie
Currently Being Moderated
Hi,
I am confused with interface. Why do we want to declare all methods in interface and defining all of them in one or more class? Also instead of declaring it in interface why cant we directly define wherever we want without declaring in interface?
  • 1. Re: Interface
    Kayaman Guru
    Currently Being Moderated
    You should do a Google search for that. That one has been explained thousands of times already.
  • 2. Re: Interface
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user10684454 wrote:
    Why do we want to declare all methods in interface
    We don't.
    We want to declare those methods in an interface other components need to call.
    And to be even more confusing: Those methods could be placed in more than one interface...

    bye
    TPD
  • 3. Re: Interface
    939520 Explorer
    Currently Being Moderated
    Searching Google for 'Java interface why' produces this article that I believe will clarify things for you.
    As per the article, you don't necessarily include all the (public) methods of your class in the interface declaration.

    http://www.javaworld.com/jw-08-1999/jw-08-interfaces.html
  • 4. Re: Interface
    rp0428 Guru
    Currently Being Moderated
    >
    Why do we want to declare all methods in interface and defining all of them in one or more class?
    >
    You don't literally declare ALL methods in an interface; just the ones that are common and need to shared/used as part of the 'contract' that specifies how the software acts.
    >
    Also instead of declaring it in interface why cant we directly define wherever we want without declaring in interface?
    >
    You can do that but then your code will have little if any polymorphism, although classes by themselves can provide some.
    >
    I am confused with interface.
    >
    Then I suggest you work your way through the Java Tutorial which has an entire section for interfaces.

    See Interfaces in the Java Tutorial
    http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
    >
    There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts
  • 5. Re: Interface
    978238 Newbie
    Currently Being Moderated
    Before you enter into interface ...first u need to understand abstract method and class.then u can easily understand and find answer for ur question..because interface is 100% abstract class
  • 6. Re: Interface
    gimbal2 Guru
    Currently Being Moderated
    975235 wrote:
    Before you enter into interface ...first u need to understand abstract method and class.then u can easily understand and find answer for ur question..because interface is 100% abstract class
    I would just learn both at the same time as they deal with the same concept of abstraction.
  • 7. Re: Interface
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    975235 wrote:
    [...] because interface is 100% abstract class
    [nitpickig]
    No.
    In Java you cannot extend multiple classes at the same time. There is only one class name allowed after extends keyword.
    But you can implement multiple interfaces at the same time. You can add as many interface names as you like after the implements keyword.
    [nitpickig]

    Also: in a 100% abstract class you cann reduce visibility of methods and you can have non static non final non public members.
    In an Interface all methods (and members) are public and all members are static final (ie. constants) even if you do not declare it so.

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points