wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the use of Interface???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use of Interface????" Watch "What is the use of Interface????" New topic
Author

What is the use of Interface????

Arun Kumar Gaddam
Ranch Hand

Joined: May 05, 2007
Posts: 58
Lets suppose That we have an interface called car

interface Car{
void driving();
void barking();}

now we implement this and make many classes like santro ,maruti etc

now my doubt we write an interface and again classes implementing these ..then wat is the use of writing an interface and then a class ..are we wasting the time???
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please ask such questions in our Java In General (beginners) Forum. But I suggest that you search for similar questions first.

Transferred from SCJP where a basic level of Java is assumed...
[ May 06, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by Arun Kumar Gaddam:
Lets suppose That we have an interface called car

interface Car{
void driving();
void barking();}

now we implement this and make many classes like santro ,maruti etc

now my doubt we write an interface and again classes implementing these ..then wat is the use of writing an interface and then a class ..are we wasting the time???


Hello Arun,

I'll try to help you but I am myself a beginner and so suggest you get hold of a good book on Java. I can recommend Head First Java but I would suggest you use it in conjunction with The Java Tutorial.

Interfaces can be used to organize the classes. For example, if you are an experienced designer and you know for sure that there are certain characteristics which cannot be changed. While there might be extra features that could be added to these basic feature set. So you could use an interface which gives the client programmer a set of rules on which to write the classes in future.

This is just one instance of using an interface and there are a lots more. As I said before they can organize your classes in a user-friendly fashion.

One more example could be 2 different sets of programmers working on a related project. So if they have a guideline which they can use as a base so that both groups know what functionality they are working on, then they can work towards a goal. This guideline could be an interface/s.

For more follow this link:
http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html

Hope this helps.
Ricky James
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39086
    
  23
Very useful. You can write and every method expecting a Car object will work.
[edit]Minor spelling correction[/edit]
[ May 06, 2007: Message edited by: Campbell Ritchie ]
Dhaval Shah
Ranch Hand

Joined: Jun 10, 2005
Posts: 56
Hi,
I would like to elaborate on what was said preiviously. Interface mainly defines a contract for the classes implementing them. In other words, taking the same example as suggested by you . . taking Car as a generic entity, there are some basic functionalities in the Car which are present irrespective of the company (who decides the way in which the functionalities has to be provided) Eg. 1. Start 2. Stop 3. Accelerate 4. Brake etc . . . . Now all these functionalities are present in all the cars . be it Mercedez, Hyundai etc . . . but the manner in which it is provided differs . . . so we declare Car as in interface.

Regards
Dhaval Shah
SCJP 1.4


Thanks and Regards,<br />Dhaval Shah<br />SCJP 1.4<br />SCWCD 1.4
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Interfaces are a limited form of design-by-contract, but mainly exist in Java because of its limited static typing. With dynamic typing or more advanced static type systems, there's no reason to have an interface, except for documentation and possible optimisation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39086
    
  23
Quoted by Dhaval Shah
Hi, . . .
Yes. Agree.
Quoted by Ricky Clarkson
Interfaces are a limited form of design-by-contract, but mainly exist in Java because of its limited static typing.
They exist mainly because a Java class cannot extend two superclasses, and they are meant to supply the flexibiltiy of multiple inheritance.
 
GeeCON Prague 2014
 
subject: What is the use of Interface????