Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 10. Interfaces

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


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint