CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
Why do you create interfaces, and when MUST you use one?
Category Java Interview Questions
Rating (3.7) By 296 users
Added on 10/13/2004
Views 22644
Rate it!
Answers:

You would create interfaces when you have two or more functionalities talking to each other. Doing it this way help you in creating a protocol between the parties involved.



interfaces are only declarations , for that can change our functionaly with greate extent



Posted by: mohan    

Contact mohan Contact mohan

Interfaces are mainly used for two purposes
first is that it removes the prob of multiple inheritence in classes.such as subclass cannot inherit the properties of two superclasses bt this can be possible by using interfaces.
secondly we use interface as a common class for all other classes.



Posted by: disha mohini pathak    

Contact disha mohini pathak Contact disha mohini pathak

Java does not support multiple inheritance. But it supports multiple implimentations. i.e. A class can implement more than one interfaces.So,we can make muliple inheritances indirectly through the interfaces



Posted by: venkatesan    

Contact venkatesan Contact venkatesan

Interfaces define only declaration of methods and final data members.We use them when we want different definitions of methods in different classes for different purposes.
To implement multiple inheritance in Java we must use interfaces in which one class can implement one or more than one interfaces.



Posted by: Afsha Khan    

Contact Afsha Khan Contact Afsha Khan

Interface is a way to provide loose coupling between the modules of an application . With the advent of frameworks like Spring this is very clear .An interface is also a very suitable way of defining a blueprint of functionality during the initial phases of a project (or more appropriately during design time).JVM and lot of frameworks/appservers use callback methods to provide run time polymorphism (Dynamic method dispatch) by using interfaces



Posted by: azad mohan kanswal    

Contact azad mohan kanswal Contact azad mohan kanswal

Interface is a tool provided by Java to achieve the multiple inheritance mechanism, which is not possible directly.
We can use interfaces to declare a set of methods that will be used in different classes for different purposes.



Posted by: sreevathsa    

Contact sreevathsa Contact sreevathsa

Interface class is the class thru which calling function interacts with implementation methods. By using virtual keyword infront of "Implementation methods" declaration, the class can become Interface class. Late(run time) binding,Encapsulation(APIs)and avoid ambiguity between multiple inheritance could be overcome with the Interface classes.



Posted by: Niranjan ambati    

Contact Niranjan ambati Contact Niranjan ambati

Interface is the class thru which calling class methods can be interacted with "Implementation class" methods. By using "virtual" keyword infront of "implementation methods" declarations in the class, become interface class. Encapsulation(APIs), avoid ambiguity between multi inheritance and late binding can be achieved thru interface class implementation..



Posted by: Niranjan Ambati    

Contact Niranjan Ambati Contact Niranjan Ambati

INTERFACE is a contract between class and the outside world and this contract is enforced at build time by the computer.
in an interface methods are public and the variables are final.



Posted by: Rohit Jain    

Contact Rohit Jain Contact Rohit Jain

Interface is a way to provide more userfriendly front end envirenmentto work on it.To clubbing many modules in a single frame we need a good interface that provides a good way to access all functionality of the system.



Posted by: shailendra    

Contact shailendra Contact shailendra

programmer goes for the interfaces when all the features(methods) have different implementations in different objects.we can also achieve multiple inheritance through interfaces.



Posted by: roshan    

Contact roshan Contact roshan

A programmer writes an interfaces , when all the features have different implementations for different objects.
Interfaces are written when the programmer wants to leave the implementations to third party venders



Posted by: Anirudh Narayan    

Contact Anirudh Narayan Contact Anirudh Narayan

Its not that we use interfaces as an option for inheritence, because when we inherite something we are including all that methods which are present in a super class, while interface just describes a prototype of the method it does not does not describe its complete body.



Posted by: Alok .V. Dethe    

Contact Alok .V. Dethe Contact Alok .V. Dethe

Interface is created when there is a need of relating two or more classes, say if we want to create relation of a sub class from two of the super classes(but practically this is not possible in java as it does not support multiple inheritance). so with the help of interfaces we can inherit the super classes.



Posted by: java_leo    

Contact java_leo Contact java_leo

interface are mainly in place of multiple inheritence and interface act as a common class to all oher classes



Posted by: Amit Raj    

Contact Amit Raj Contact Amit Raj

Interfaces are used in Java to declare functinalities of entities which may have same functinalities but implemented them in different ways, and in this way conforming them to a standard defined in the interface. For example ( completely illusive ) ,
an authority in a country wants to create a standard for automobiles with bare minimum functionalities for car movement ( fwd, rear, move left, move right ) . Let's assume the interface as -

interface MoveCar
{
fwd();
rear();
left();
right();
}

Now it's the job of the car companies to conform this standards and implement these minimum functionalities if not more. But the standard only tells what needs to be done without forcing how it needs to be done. In this way we can have multiple
standards for different component in a product and thus can implement multiple interfaces in a single implementation class. So at the end of the day it declares a protocol but leaves the details of the implementation to the implementor.



Posted by: Swagatam Mitra    

Contact Swagatam Mitra Contact Swagatam Mitra

There are two main reason two create an interface :
One when you want to notify the behavior by just give a name to it, this is called marking of the class and the interface called the Marker Interface. For ex. Serializable, Cloneable,

The other when you want a set of method that confirming the implementing class must must must have.

The interface then will act as a bridge between the different components which can define the body of the client class with respect to their own need.



Posted by: Chirag Mehta    

Contact Chirag Mehta Contact Chirag Mehta

in java there is no multiple inheritance,so to achieve multiple inheritance in java we must use interface concept



Posted by: santosh kumar    

Contact santosh kumar Contact santosh kumar

Interface is a way to provide loose coupling between the modules of an application . With the advent of frameworks like Spring this is very clear .An interface is also a very suitable way of defining a blueprint of functionality during the initial phases of a project (or more appropriately during design time).JVM and lot of frameworks/appservers use callback methods to provide run time polymorphism (Dynamic method dispatch) by using interfaces



Posted by: ravichandra rayipudi    

Contact ravichandra rayipudi Contact ravichandra rayipudi

You would create interfaces when you have two or more functionalities talking to each other. Doing it this way help you in creating a protocol between the parties involved.
Interfaces are mainly used for two purposes
first is that it removes the prob of multiple inheritence in classes.such as subclass cannot inherit the properties of two superclasses bt this can be possible by using interfaces.
secondly we use interface as a common class for all other classes.



Posted by: Anuj Pandey    

Contact Anuj Pandey Contact Anuj Pandey

Interface is just like special classes. in this is use only declare memeber function not definition. it is use as a templets. when we want to call which function then declare function defination in his class.



Posted by: CHINTU    

Contact CHINTU Contact CHINTU

it is used to declare abstract classes and symbolic notation like 3.14 we can extend it through implementation.



Posted by: packiya    

Contact packiya Contact packiya

Mainly we use interfaces for two reasons
first is when we want to give a common behaviour to more than one classes in our application.
second is that when we want to use multiple inheritence.



Posted by: Prasad    

Contact Prasad Contact Prasad

iterface is used make the connection between two function and make it easy to implement by class



Posted by: rahul setia    

Contact rahul setia Contact rahul setia

Interface provides an abstract form of a method that can by further implemented by their implemented Class. Methods in an interface are the generalized methods and implementation is depend upon their subclass behavior. Further to provide a feel of multiple inheritance in terms of Interface which is not possible with Java Classes.



Posted by: Abhishek Singh    

Contact Abhishek Singh Contact Abhishek Singh

You would create interfaces when you have two or more functionalities talking to each other. Doing it this way help you in creating a protocol between the parties involved.



Posted by: manish mishra    

Contact manish mishra Contact manish mishra

Only method declaration is possible in interface.But in abstract class it is possible to define methods.



Posted by: shyju    

Contact shyju Contact shyju

Only method declaration is possible in interface.But in abstract class it is possible to define methods.



Posted by: shyju    

Contact shyju Contact shyju

Interfaces provide basic behavior for an application/module which can be implemented/improved further by the classes which are going to implement the interface, and an application with interfaces will always have multiple inheritance facility.



Posted by: Arun Kumar R    

Contact Arun Kumar R Contact Arun Kumar R

In java interface is used for two main purpose
1.To implement complete abstraction as in interface methods are declared but never defined.Even the abstract class in java cannot provide complete abstraction because it can contain abstract as aell as concrete method
2.To implement multiple inheritance which is not possible with the help of classes in java



Posted by: Kadambri Agarwal    

Contact Kadambri Agarwal Contact Kadambri Agarwal

Interface in java used in two purposes.
1. it avoids the problem of multiple inheritence in classes. interfaces can be used in java, classes can be indirectely inheriting.
2.Interfaces define only declaration of methods and final data members.We use them when we want different definitions of methods in different classes for different purposes.



Posted by: Prajith M Kerala    

Contact Prajith M Kerala Contact Prajith M Kerala

Interface is the colletion of method declaration.only declaration not defination ,so interface solve the multipul inheritance prob,if you want to inherit multipul method you can use interface.



Posted by: Panakj Rajput    

Contact Panakj Rajput Contact Panakj Rajput

We can use interfaces to declare a set of methods that will be used in different classes for different purposes.



Posted by: koteshwar    

Contact koteshwar Contact koteshwar

Incase of using same method for different functionality we have to use interface.



Posted by: Prasann Kumar Mishra    

Contact Prasann Kumar Mishra Contact Prasann Kumar Mishra

we create interface bcoz ...
1. To implement multiple inheritance in java.
2. To advertise the compiler that class implementing that interface is ready to deliver the functionality defined by the interface.

we use the interface in cases where the classes need to define the same method but the way of execution is different for every classes depending on their need.

eg:- If we want to sort between two employee object on the basis of salary we have to implement the comparable interface and define the compareTo()such that it sorts on salary .

but if we want to sort two manager object on age basis we again have to implement the comparable interface and define the compareTo() method but the code written inside the compareTo() method is now different from the above one .
Now both implement the same interface and define the same method but still the way of execution is different .One sort on age another on salary .implementing comparable interface advertise the compiler that class implementing this interface will compare data but the basis of comparison depends upon the code written in compareTo().



Posted by: Sourav    

Contact Sourav Contact Sourav

encapsulation implies program and codes in single unit



Posted by: k.thangamuthu    

Contact k.thangamuthu Contact k.thangamuthu

Through interface one class can inherit the properties of multiple classes in multiple inheritence trees.Its also possible through multiple inheritence but complexity increases.thats why java does not allow multiple inheritence but indirectly give the facility through interface.



Posted by: subhrajeet     

Contact subhrajeet Contact subhrajeet

we create interface
to implement multiple inheritance in java.



Posted by: sree    

Contact sree Contact sree

Interface is a blueprint for implementing several types of methods in different classes.In project implementation initially lead people think about the Interfaces and they ll design.and also multiple inheritance is also possible.



Posted by: raghavan p    

Contact raghavan p Contact raghavan p

Interface is a blue print which has methods declaration and final variables. It is common lass for many other class. Interface is created to implement the multiple inheritance which java doesn't support directly. Interface will be implemented by different classes by defining the methods differently for different purpose.



Posted by: Ravi Babu    

Contact Ravi Babu Contact Ravi Babu

java is said to be it is not fully object oriented because it doesn't support Multiple inheritance.
But it provide a facility called Interface to implement multiple inheritance.



Posted by: dhanunjay    

Contact dhanunjay Contact dhanunjay

interface used for
1)multiple inheritance
2) designing purpose
designing means once you implement it
you have to override its method
it give you a standard way of programing



Posted by: vikram    

Contact vikram Contact vikram

In order to implement the multiple level inheritance, we use interface. Interface actually is used in JAVA to inherit the property of multiple classes.

Whenever the methods of one class are inherited in another class, the JAVA compiler checks whether the methods are compatible and they can be inherited, this problem is solved in interface where the methods are easily inherited in any class.



Posted by: Ajay Raina    

Contact Ajay Raina Contact Ajay Raina

As java does not support the concept of multiple inheritance, i.e derived class inheriting properties of more than one base class,

Do to over this ambiguity java support the concept of INTERFACE(keyeword). where a class can implement properties from more then one base class.

Interface siddh
{
final datavar=value;
void show();
}
NOTE:
we should define the function the should only be declared the class that implements it provide the defination for this method SHOD().

Variable declared are by defaul final so that implementing class cannot change the value.



Posted by: siddharth    

Contact siddharth Contact siddharth

For any project you have first decided the design of your project.
You are giving your project to others,
then you must want some methods to be included by everyone,
so create a interface declaring those methods that must be implemented by everyone.
you may also use interface to define some constants which should be final and static.



Posted by: sonika    

Contact sonika Contact sonika

You would create interfaces when you have two or more functionalities talking to each other. Doing it this way help you in creating a protocol between the parties involved.For any project you have first decided the design of your project.
You are giving your project to others,
then you must want some methods to be included by everyone,
so create a interface declaring those methods that must be implemented by everyone.
you may also use interface to define some constants which should be final and static.






Posted by: Raju.R    

Contact Raju.R Contact Raju.R

In java does not support to multiple
inheritance this problem we solve by using "Interface" in subclass we can not inherit properties of two super class but we can implement two Interface.



Posted by: devdatta shinde    

Contact devdatta shinde Contact devdatta shinde

Firstly i would like to tell that interface is not about to get multiple inheritance indirectly.
Interface are the blueprints that shows the way what to do but it does not show how to do. I mean it just declares methods but does not define.
Now come to the second part when we have need to implement interface. If we are using any of the method of the interface directly or indirectly (by hierarchy of inheritance) then we must implement the corresponding interface.



Posted by: sonu solanki    

Contact sonu solanki Contact sonu solanki

interface is used to support multiple inheritance.The main advantages of declaring variables with in an interface is that they become globally available to all the classes and need not be declared explicitly in classes implementing them.



Posted by: Deepti Priyadarshini    

Contact Deepti Priyadarshini Contact Deepti Priyadarshini

Interface is basicaly used to solve the problem of multiple inheritence.Because java does not support multiple inheritance but it supports multiple implementation.a class can not extend more than One class but can implement more than One Interface.



Posted by: Shiva Ram    

Contact Shiva Ram Contact Shiva Ram

Java does not support multiple inheritance.Interface is basicaly used to solve the problem of multiple inheritence.



Posted by: Balaji    

Contact Balaji Contact Balaji

Now in the Java language, a programer can actually write a piece of code, whose sole purpose is to define what methods and variables a class contains. This, is done with the keyword ?interface?. Once a interface is defined, other classes can say which interfaces they implement, so that if class C implement interface I, then programers don't need to know the details about C. All they need to know is the interface I. (which specifies all the methods, constructors, variables, a class must have.)



Posted by: arjoo kumar    

Contact arjoo kumar Contact arjoo kumar

You would create interfaces when you have two or more functionalities talking to each other.

Interface is used make the connection between two function and make it easy to implement by class

Interfaces are mainly used for two purposes

1. First is that it removes the prob of multiple inheritence in classes.

2. we use interface as a common class for all other classes.




Posted by: Mahesh    

Contact Mahesh Contact Mahesh

Interfaces are also abstract classes, which is useful to redeclaration the classes.



Posted by: rajesh    

Contact rajesh Contact rajesh

we create intefaces for act as a mediTOR B/W the programmes.like two resturents are there the menu is same in any restuarent if the menu is interface it wont change even the restuarent changes.it implements the loose coupling.



Posted by: naresh    

Contact naresh Contact naresh

Interface is used for its functions to be implemented.

The interface function does not contain any function description. Only declaration is there.

eg.

class xyz extends JFrame implements ActionLister

here ActionLister is an Interface and actionPerformed() is a blank funtion which can be triggered while any action is being fired in the method.



Posted by: Prantim Gautam    

Contact Prantim Gautam Contact Prantim Gautam

Interface is used for its functions to be implemented.

The interface function does not contain any function description. Only declaration is there.

eg.

class xyz extends JFrame implements ActionLister

here ActionLister is an Interface and actionPerformed() is a blank funtion which can be triggered while any action is being fired in the method.



Posted by: Prantik Gautam    

Contact Prantik Gautam Contact Prantik Gautam

Interface is specification of method prototypes.
Interface are use use to implement multiple functionalities based upon requirementt
Interface are used when multiple objects share different feature
e.g suppose a s/w organisation is implementing application which can connect multiple data bases to the applications then oe interface is used to connect oracle database and othe for cybase database.



Posted by: raghavender reddy    

Contact raghavender reddy Contact raghavender reddy

Interface is specification of method prototypes.
Interface are use use to implement multiple functionalities based upon requirementt
Interface are used when multiple objects share different feature
e.g suppose a s/w organisation is implementing application which can connect multiple data bases to the applications then oe interface is used to connect oracle database and othe for cybase database.



Posted by: Ritesh raj    

Contact Ritesh raj Contact Ritesh raj

interface is a kind of abstract class without abstract keyword but difference is this that abstract class having methods abstract & with body but interface having only methods without body(by default public & abstract) & and we can't inherit abstract class but interface can be it is also useful to remove the problem of multiple inheritance & we can't create the instance of interface but we can create the reference variable of interface who's hold the address/reference of extended class . if any class inherit the interface it is mandatory to define all methods of interface otherwise it will be error . if we create interface & if we include variable in interface it is mandatory to initialize because of variable of interface are by default fine public static & interface can also inherit other interface but interface can't inherit any class at all . there are lot of other description about interface .pl z e-mail me to any query thanks



Posted by: gurpreet singh    

Contact gurpreet singh Contact gurpreet singh

Mainly java language does not support multiple inheritance but it provides multiple implementations by using interfaces.In interfaces we will provide declarations and so interfaces can be extended.



Posted by: alekya    

Contact  alekya Contact alekya

Interface is used when we provide some functionality from one class to another class then we use interface as a mediate between two class that prove only those feature whose we want to provide in that class



Posted by: mukesh singh    

Contact mukesh singh Contact mukesh singh

interface is design overview of software for sofwtware designer
according to which programer can code the software



Posted by: yesurkar kishor    

Contact yesurkar kishor Contact yesurkar kishor

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    What is the difference between instanceof and isInstance?
    View Answer
    Are there any other 'marker' interfaces?
    View Answer
    How many methods do u implement if implement the Serializable Interface?
    View Answer
    What are the advantages of OOPL?
    View Answer
    What do mean by polymorphism, inheritance, encapsulation?
    View Answer
    What do you mean by static methods?
    View Answer
    What do you mean by virtual methods?
    View Answer
    Given two tables Student(SID, Name, Course) and Level(SID, level) write the SQL statement to get the name and SID of the student who are taking course = 3
    and at freshman level.
    View Answer
    What are the disadvantages of using threads?
    View Answer
    Write the Java code to declare any constant (say gravitational constant) and to get its value.
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL Java Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/378/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

    1.27
    Cache = 0 Seconds