dcsimg
What is the purpose of Interface?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   vijay_varanasi
Posted On:   Tuesday, April 29, 2003 06:56 AM


What is the purpose of Interface?
What is the main use of interface in Java Language.
How do we get Polymorphism by using the interfaces????

Re: What is the purpose of Interface?

Posted By:   Anonymous  
Posted On:   Friday, June 6, 2003 02:44 AM

About Polymorphism, your question should not be "How do we get Polymorphism by using the interfaces?", but: How would we have polymorphism without Interfaces?



Java is a strongly typed language. Therefore you have to know the type of the object you manipulate at any time. This is especially true in methods return type and parameters types. So, if you would like to receive objects as parameters, coming from different hierarchies, the ONLY way to do that is with Interfaces. Otherwise you are stuck with object from the same branch of the same hierarchy.
So, Interfaces are mandatory if you want to have real polymorphism in Java.



With a language like Smalltalk, as it is not typed, you do not need Interfaces as you can receive any type of object anywhere. But because there is not verification possible by the compiler, you would have troubles at run time if you pass a wrong object.



I hope I am clear.



S.L.

www.liemur.com

Re: What is the purpose of Interface?

Posted By:   zenykx_pope  
Posted On:   Sunday, June 1, 2003 12:17 AM

... also think of it as a sure way to use multiple inheritance.

Re: What is the purpose of Interface?

Posted By:   Simon_Ablett  
Posted On:   Tuesday, April 29, 2003 09:44 AM

An Interface is a statement of intent; a contract as it were. By implementing an interface a class is saying that it will provide this behaviour i.e. that it will implement these methods. In OO terminology it is an 'implemented as' relationship rather than an 'is a' one. i.e. 'class A is implemented in terms of interface B' rather than 'class A is an instance of class B'.

Not sure if this information is of use to you.

Regards.

Re: What is the purpose of Interface?

Posted By:   Brian_Kapellusch  
Posted On:   Tuesday, April 29, 2003 07:32 AM

Interfaces allow you to treat a group of classes in the same manner. Take, for example, the Comparable interface. To put in object in a SortedSet, you must implement this interface. A Car object and a Cow object many not be anything alike, but they might both have their own unique way of sorting.

Polymorphism is simply a term that refers to the ability to process objects differently, depending on their class. For example, a Car and a Cow object might both implement the Mobile interface. You want to tell both objects to .move(), not caring about how these objects move.

Make sense?
About | Sitemap | Contact