What is An Interface?

India
March 9, 2007 4:24am CST
What is an interface in java...how it is used? Y it is used?
4 responses
• India
10 Mar 07
An interface is a pure abstract class. That means it contains only abstract methods .And an interface needs to implemented rather than extended like other classes. The interface only contains empty definitions of functions but the implemented class needs to write the full code for those functions.. ........hope u are clear with this answer incase of any further doubts u can ask me
2 people like this
@raghwagh (1527)
• India
10 Mar 07
Interface is a special type of class that contains only abstract methods and data members.Means an interface only contains method signatures and the datamembers.For using the interface we have to implement it in a class and then override the methods and use it.Mainly interface is used to get the feature of multiple inheritance in java, which is directly not possible. example public interface SampleInterface { int p,q; Double d; public void methodOne(); public void methodTwo(int a,int b); } //using interface public class ImplementInterface implement SampleInterface { public void methodOne() { System.out.println("In methods one"); } public void methodTwo(int a,int b) { System.out.println("In methods two with a:"+a+" and b:"+b); } }
1 person likes this
@jhawithu (1070)
• India
20 Apr 07
As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off. In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows: interface Bicycle { void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } To implement this interface, the name of your class would change (to ACMEBicycle, for example), and you'd use the implements keyword in the class declaration: class ACMEBicycle implements Bicycle { // remainder of this class implemented as before } 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.
• India
10 Mar 07
using the keyword interface,you can fully abstract a class interface from its implementation.that is ,using interface,you can specify what a class must do,but not how it does it.interfaces are syntatically similar to classes,but they lack instance variables,and thier methods are declared without any body.in practice this means that u can define interfaces which do not make assumptions about how they are implemented.once it is defined,any no of classes can implementan interface.also one class can implement any no of interfaces