Object-oriented language basics, Part 6
Use interfaces for safe multiple inheritance and a great deal more
By Jeff Friesen, JavaWorld.com, 09/07/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Java's interface language feature confuses many Java newbies. New developers often assume that interfaces only sidestep Java's
failure to support multiple implementation inheritance. However, that workaround is only a side effect of interface usage,
as you will discover in this article.
If you follow the Java 101 column, you know that we are currently touring Java's object-oriented language basics. So far, this series has covered class
declaration, object creation, field/method (including constructor method) declaration and access, object composition, object
layering, multiple implementation inheritance, and the root of all classes. In addition, I have introduced the specialized
topics of enumerated types and singletons. Read the whole series on object-oriented language basics, and learn what it means
for a Java program to be object-oriented:
Part 6 of this series presents a deeper understanding of interfaces.
Interfaces
The word interface conjures up the image of a place where two independent systems meet for communication. For example, a human being and a computer
communicate through a keyboard, a mouse, and/or some other input device. The input device serves as an interface between the
computer and the human being. From a software perspective, an interface represents a service contract between a library that
implements those services and code that calls upon that library's services. For example, a Java program obtains file I/O (input/output)
services by creating objects from various file-related classes in Java's class library and calling their methods. Those classes
and methods form an interface between code wanting file I/O and the actual file I/O code.
We can take the interface concept one step further: from Java's perspective, an interface is the introduction (but not an implementation) of a type at the source code level. To understand that concept, you first
must grasp the notion of type, an abstraction that identifies a set of data items -- that share some commonality -- and a set of operations that manipulate
those data items. For example, the integer type identifies a set of numbers that do not have fractional parts and a set of
operations that manipulate those numbers (such as addition and subtraction). To support types, a computer language provides
a facility that allows a developer to introduce (and possibly implement) a type in source code.
Developers introduce primitive types into source code through keywords (such as int or boolean) and operators (such as + or !). Primitive types have no implementation at the source code level: the compiler and the JVM provide an implementation. Developers
introduce reference types into source code through class and interface declarations. Classes implement reference types (at
the source code level) by declaring read/write field variables (to identify a set of data items) and method signatures with
code bodies (to identify a set of operations). Unlike classes, interfaces do not implement reference types in source code.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- For a glossary specific to this article, homework, and more, see the Java 101 study guide that accompanies this article
http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-java101guide.html
- Download this article's source code and resource files
http://www.javaworld.com/javaworld/jw-09-2001/java101/jw-0907-java101.zip
- The Java Language Specification, Second Edition, James Gosling, Bill Joy, Guy Steele, Gilad Bracha (Sun Microsystems, 2000)
http://www.javasoft.com/docs/books/jls/second_edition/html/j.title.doc.html
- The Java Tutorial
http://www.javasoft.com/tutorial/
- The Javadoc Tips FAQ
http://java.sun.com/j2se/javadoc/faq.html
- Javadoc 1.3 Documentation
http://java.sun.com/j2se/1.3/docs/tooldocs/javadoc/index.html
- Sun's official Java language definition
http://java.sun.com/docs/overviews/java/java-overview-1.html
- "A Primordial Interface?" Wm. Paul Rogers (JavaWorld, March 2001) -- Java's Object class is the root of all classes. Does Java have a similar root interface?
http://www.javaworld.com/javaworld/jw-03-2001/jw-0309-primordial.html
- "Constants, I Do Declare," Tony Sintes (JavaWorld, June 2001) -- Some developers feel that constants should not be declared in interfaces
http://www.javaworld.com/javaworld/javaqa/2001-06/01-qa-0608-constants.html
- "Java Diamonds Are Forever," Tony Sintes (JavaWorld, March 2001) -- How does Java use interfaces to solve the famous diamond problem?
http://www.javaworld.com/javaworld/javaqa/2001-03/02-qa-0323-diamond.html
- "Java Tip 10Implement Callback Routines in Java," John D. Mitchell (JavaWorld) -- You can use interfaces to implement the equivalent of callback functions in Java
http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
- Check out past Java 101 articles
http://www.javaworld.com/columns/jw-101-index.shtml
- Java experts answer your toughest Java questions in JavaWorld's Java Q&A column
http://www.javaworld.com/javaworld/javaqa/javaqa-index.html
- For more articles on Object-Oriented Design and Programming, browse our Topical Index
http://www.javaworld.com/channel_content/jw-oop-index.shtml
- For Tips 'N' Tricks see
http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html
- Sign up for the JavaWorld 's free weekly email newsletters
http://www.idg.net/jw-subscribe
- You'll find a wealth of IT-related articles from our sister publications at IDG.net