Java Reference
In-Depth Information
Interfaces
Interfaces are a template for creating classes. The template defines a common structure
which can be used to create many classes with similar structure. Interfaces are just template
of a structure and so can not be used in implementation. Classes which use this structure
template provided by the interface implement the structure.
Public interface my_interface {
Public void yellow();
}
Now you can implement a class based on this interface:
Public class my_class implements my_interface {
Public void yellow(){
String color = “yellow”;
system.out.println (color);
}
}
You can create many classes which can have similar structure as the interface by implement-
ing this interface.
Interfaces can be used to make 2 dissimilar classes to behave similarly. For example consider
you have addresses of people as well as companies. You also have name of people and com-
panies. If you look at how names and addresses are written for companies and people, you
will see that there are some differences.
First Name: Ian
Last name: Anderson
Address line 1: 123 Canopy Island
Address line 2: New York City
Company name: Fantastic Technologies
Address line 1: 456 Canary Wharf
Address line 2: Los Angeles
As you can see for name of people there are 2 fields: first name and last name. But for com-
panies there is just one field company name. If you want to implement it so that you will be
Search WWH ::




Custom Search