www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Java Interfaces Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
As mentioned earlier, multiple inheritance is directly not allowed in Java, however, the same could be achieved through the concept of Interface in Java. Inheritance implements the "is-a" relationship and an interface is similar to a class, but can only contain public, static, final fields (i.e., constants) and public abstract methods (i.e., just method headers, with no bodies).

An interface is declared as shown below:
public interface Employee { 
   void RaiseSalary( double d ); 
   double GetSalary(); 
}
Note that both methods are implicitly public and abstract (those keywords can be provided, but are not necessary in an interface).

A class can implement one or more interfaces (in addition to extending one class). It must provide bodies for all of the methods declared in the interface, or else it must be abstract. For example:
public class TA implements Employee {
   void RaiseSalary( double d ) { 
   // actual code here 
   } 
   double GetSalary() { 
   // actual code here 
   } 
}
Public interfaces (like public classes) must be in a file with the same name. A single class can implement many interfaces at the same time thus achieving multiple inheritance.

An interface can also be used as a "marker" with no fields or methods. A marker interface is used only to "mark" a class as having a property, and is testable via the instanceof operator.
Next >>> Lesson No. 13: Type Conversion in Java







Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.