Error » Certification & Programming center Error !! » Programming tutorials » Interfaces in Java

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Interfaces in Java
LinkBack Thread Tools Display Modes
Old 03-Jan-2007, 10:29 PM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Interfaces in Java

In Java, a class can have at the most one immediate superclass. Multiple inheritance, where a class has more than one superclass, is not allowed in Java. However, one cannot ignore the importance of multiple inheritance because a large number of real-life applications require the use of multiple inheritance. Java provides an alternative approach to support multiple inheritance by introducing a new data type known as interfaces. Although a class cannot inherit multiple classes, it can implement more than one interface. An interface declaration in Java introduces a new data type.

An interface is a named collection of abstract methods and constants. Interfaces are syntactically similar to classes, except that they contain only abstract method declarations and constants. They are defined with a source code file with the .java extension that is compiled to a class file in the same way as classes. In Java, interfaces provide a convenient alternative to multiple inheritance.

The body of an interface declares a set of methods but does not provide their implementations. A class that implements an interface has a legal binding to implement all the methods defined in the interface, thereby agreeing to a certain behavior. A class can be declared to implement any number of interfaces by using the keyword implements followed by one or more interfaces.

Defining an Interface

An interface is defined using the syntax given below:access_modifier interface interface_name extends Super_interfaces{
return_type method1(parameter_list);
.
.
.
return_type methodn(parameter_list);
datatype variable1 = value;
.
.
.
datatype variablen = value;
}

The syntax defined above has two components: the interface declaration, and the body of the interface.

They are discussed in the following sections:

The interface declarationAn interface declaration consists of the following components:

·access_modifier: An interface can be declared by using the public access modifier or without any access modifier. When an interface is declared as public, it can be accessed by any class in any package. If no access modifier is used in the declaration, the interface is accessible only within the package in which it is declared.

·interface interface_name: The keyword interface in the declaration tells the compiler that it is an interface declaration and name of the interface is the one that follows the interface keyword. The name of an interface must be a valid identifier.

·extends Super_interfaces: An interface can extend other interfaces in the same way as a class extends another class. The only difference between these two is that where a class is allowed to extend only one class, an interface can extend any number of interfaces. The extends keyword in an interface declaration tells the compiler that the interface extends the list of interfaces that follows the extends keyword.

The interface body

The interface body contains method declarations for all the methods included in the interface. A method declared within an interface does not have a body, i.e., the method declaration ends with a semicolon. This is because an interface does not provide implementations for the methods declared within it. Apart from method declarations, an interface contains constant declarations.

All the constants defined in an interface are by default static and final. Therefore, there is no need to explicitly use these keywords to declare variables inside the interface body. All methods and variables in an interface are implicitly public if the interface is declared as public. It is also not necessary to declare the methods as abstract as well.Member declarations in an interface disallow the use of some declaration modifiers, i.e., the following modifiers cannot be used to declare members in an interface:

· transient
· volatile
· synchronized

Also, the private and protected access modifiers cannot be used to declare members of an interface. The following example demonstrates how to define an interface:

public interface MyInterface{
void method1();
int method2(int k);
int x = 1;
int y = 2;
}

Implementing Interfaces: When an interface has been defined, any number of classes can implement it. A class implements an interface by using the implements keyword followed by the interface name in its declaration part. A class that is declared to implement an interface provides definitions of all the methods declared in the interface. The following example shows the structure of a class named F0014 that implements the java.lang.

Runnable interface:

public class F0014 implements Runnable{

public void run(){
//statements
}
}
The Runnable interface is defined in the java.lang package. This interface declares a single method with the following signatureublic void run();The F0014 class implements it by using the implements keyword followed by the interface name, and then provides the definition of the run() method with the following signature:

public void run(){
}

The run() method implemented by the class must be declared as public with a return type of void or else a compile-time error will occur.A class can implement any number of interfaces at a time. Considering this case, if the F00014 class wants to implement more than one interface, a list of interfaces can be put after the implements keyword with a comma in between two interfaces.

Partial implementations of an interface by a class:

If a class implements an interface but does not provide implementations for all the methods defined in that interface, that class must itself be declared as abstract or else a compile-time error will occur.

For example,
abstract class F0014A implements MyInterface{ int s, t; public void method1(){
}
}

Here, the class does not implement all the methods declared in the MyInterface interface (defined earlier) and hence is declared as abstract.

An interface can extend another interface:

An interface can inherit another interface by including in its declaration the extends keyword followed by interface name (to be inherited) in the same way as a class extends another class. When a class implements such an interface (i.e., an interface that extends another interface), it must provide implementations for all the methods defined in the interface as well as methods defined in the superinterface.
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -8. The time now is 09:51 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228