interface

in interface we are providing only method signature without implementation we are using those methods and implementing in another classes.my doubt is that what is the need we can directly use declare and implement methods in classes so why we are using interfaces

thank you

with regards

swapna

[319 byte] By [reetua] at [2007-11-27 2:55:32]
# 1
Interfaces are used to : 1 - hide the implementations in the classes. 2 - simulate multiple inheritence in java 3 - ...see http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html
java_2006a at 2007-7-12 3:32:23 > top of Java-index,Java Essentials,Java Programming...
# 2

an interface in the java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement interfaces are declared using the interface keyword and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final)

as interfaces are abstract they cannot be directly instantiated object references in java may be specified to be of an interface type; in which case they must either be null or be bound to an object which implements the interface the keyword implements is used to declare that a given class implements an interface a class which implements an interface must either implement all methods in the interface or be an abstract class one benefit of using interfaces is that they simulate multiple inheritance all classes in java (other than javalangobject the root class of the java type system) must have exactly one base class; multiple inheritance of classes is not allowed however a java class may implement any number of interfaces interfaces are used to collect like similarities which classes of various types share but do not necessarily constitute a class relationship for instance a human and a parrot can both whistle however it would not make sense to represent humans and parrots as subclasses of a whistler class rather they would most likely be subclasses of an animal class (likely with intermediate classes) but would both implement the whistler interface another use of interfaces is being able to use an object without knowing its type of class but rather only that it implements a certain interface for instance if one were annoyed by a whistling noise one may not know whether it is a human or a parrot all that could be determined is that a whistler is whistling in a more practical example a sorting algorithm may expect an object of type comparable thus it knows that the object's type can somehow be sorted but it is irrelevant what the type of the object is finally interfaces may be used in place of multiple inheritance of classes by the way please use punctuation it makes text easier to read and also you can look basic material like this in wikipedia or sun's online tutorials they explain the matter quite well

jsalonena at 2007-7-12 3:32:23 > top of Java-index,Java Essentials,Java Programming...
# 3

Interfaces are used to state an standard of which metods that are supposed to be in the classes that implements it. That is so if you wanna exchange the class your currently using(for example an console writer class) for another class(an class that writes to an file for example), then you should be able to do it without refactoring.

By doing as in the example bellow you can easy change to writing to an file instead of writing to the console by just changing the class your using. By doing this you will also make it so that only metods that are stated in the interface is possible to use.

Consider all the needed refactoring you would have to do to the code if you desided that in the middle of your 1000rows long code that your gonna start writing everything to an file instead of the console. By using this interface you will now instead only need to change 1 row instead of possible houndreds of rows that states write to console.

That row is the row stating which class you wanna use.

Here is an examplecode, also note that the extra metod in consolewriter aint possible to use, as it aint stated in the interface.

interface WriterInterface {

public void writeText(String mssg);

}

class ConsoleWriter implements WriterInterface {

public void writeText(String mssg) {

System.out.println(mssg);

}

public void someThingElse() {

//Some code

}

}

class FileWriter implements WriterInterface {

public void writeText(String mssg) {

//Code for writing to file

}

}

public class Writer {

public static void main(String[] args) {

WriterInterface wi = new ConsoleWriter();

wi.writeText("Some text to be written to console");

wi = new FileWriter();

wi.writeText("Text to be written to file");

}

}

Hope this helped a bit to understand the major purpose of using interfaces.

prigasa at 2007-7-12 3:32:23 > top of Java-index,Java Essentials,Java Programming...
# 4

> Interfaces are used to :

> 1 - hide the implementations in the classes.

> 2 - simulate multiple inheritence in java

> 3 - ...

>

> see

> http://java.sun.com/docs/books/tutorial/java/IandI/cre

> ateinterface.html

why do you think the use of interfaces is a simulation of multiple inheritance? it is multiple inheritance, of type. which is more useful than implementation inheritance, anyway, multiple or otherwise

georgemca at 2007-7-12 3:32:23 > top of Java-index,Java Essentials,Java Programming...