Chapter 10. Interfaces


An interface is another general programming construct provided by the Java language. An interface, in essence, is a named block of code that contains method definitions and constant variable declarations. An interface declares abstract methods that must be implemented by any class that implements the interface. The interface does not implement the methods , it defines the method name , return type, and input parameter list. An interface can also define constants that can be accessed by classes that implement the interface.

In some ways the function of an interface is similar to that of an abstract class. An interface specifies the methods a class must implement, but not how to implement them. A class that implements an interface can implement the interface methods any way it likes as long as the method name, return type, and input parameter list are the same. In this way, an interface is another example of polymorphism. Interfaces can give you clues about the nature of the classes that implement them. For instance, you can infer with confidence that an instance of a class that implements the IComparable interface is an object that is meant to be compared against something else.

In this chapter we will discuss ”

  • Differences between interfaces and abstract classes

  • Declaring an interface

  • Interface members

  • Implementing an interface

  • Interfaces and inheritance

  • Interface instances as input parameters and return types



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net