why do we need interface in java
I was also thinking about how interfaces are used. I hope this will help others:
An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation. Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass.
This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass? ). However, multiple inheritance does have its place. Java does this by permitting you to "implements" more than one interfaces (but you can only "extends" from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error. ) from: Showing 1 - 13 of 13 Ans Guest Answered On : Jun 29th, 2006 Java don't support Multiple Inheritance. For that we can use Interface. Interface is like the getway to comunicate between to classes. Not always we write methods in Interface, we can also create objects in the Interface. The Object which will be shared between two classes can be created in an Interface. Creating Seperate Interface is one of the key factor of Software Engineering.
Suppose Class "A" has many methods depends on several Classes' objects. Think that, one method creates connection depends on Class B object and another method makes business rules depending on Class C object. In this situation, you can actually use to Seperate Interface for better performence. rabbi Answered On : Jun 29th, 2006 Some times you want to hide your detail implementation, you can use interface as a representation of a class and interface method is like abstract so implementation depends on the client class. Pranita Answered On : Jul 17th, 2006 Interface acts as communicator for the classes. since interface contains many empty bodies in it so diffrent implementations can be provided for same method as per its use in diffrent classes so interfaces are needed. sujatham Answered On : Jul 29th, 2007 Without interfaces multiple inheritence is not possible in java. sampra Answered On : Feb 22nd, 2008 vkg_16my Answered On : Mar 1st, 2008 Interface is the contract between two party where one party will implement to that contract and other party will use that contract to invoke the implementation. that means contract will remane same but implementation can vary freely.
Example -- collection calsses, RMI. sampra Answered On : Mar 6th, 2008 ankit. mca4 Answered On : Mar 28th, 2009 A class can contain multiple interface. If B class want to define two methods of class A and class C wants three methods of class A then class A implements two interface one for B and another for C. kajal Answered On : Sep 28th, 2011 If we want the concept of multiple inheritance in java than we use interface. dust Answered On : Oct 25th, 2011 Interface basically provides a hierarchy of methods so we only implement that particular interface instead of making the instance of that. ok sampra Answered On : Mar 6th, 2012 Java dont support Multiple Inheritance. For that we can use Interface. punyamca Answered On : Feb 6th, 2015 For late binding purpose. to design a method so that a part of functionality has to be mentioned by end user(implementing class) Bhavesh Answered On : Feb 28th, 2015 To get the hierarchy of classes that I might need later.
- Views: 27
why do we use void main in c programming
why do we use interfaces in java
why do we use interface in java
why do we use abstract class in java
why do we need interfaces in java
why do you need an audio interface
why do we use interfaces in java