Java classes can have only a single superclass (no multiple inheritance), they inherit variables and methods from that superclass and all of its superclasses.
This is actually a matter of much discussion. Some argue adamantly for multiple inheritance, and some argue adamantly against it.
Those that argue against say that multiple inheritance is often mis-used and that quite often the "inheritance" isn't necessarily multiple IS-A relationships (always always always test inheritance with the IS-A test...fairly). In OO, inheritance means the acquisition of all the characteristics of its immediate ancestor. What we really have are ontological statements: what is it really?
Consider some examples:
Ok, whether you are sold or not, Java does not allow multiple inheritance.
They say that a better idea is to use interfaces
Suppose you wanted to write general routine(s) that would work on many different types of objects... but you are not concerned with the state of the object - the variables always stay the same
Interfaces, like abstract classes, provide templates of behavior that other classes are expected to implement.
Java's interfaces are designed to solve dynamic method resolution at runtime.
Definition: An interface is a collection of method names, without actual definitions, that indicate that a class has a set of behaviors in addition to the behaviors the class gets from its superclass.
The major difference between interface and abstract is that an interface provides a means of encapsulating method protocols without forcing the user to subclass.
An example:
Interfaces: More like uses than isa
Provides design - if one has an audioClip, what do I expect it to be able to do?
What do I expect as behaviors?
Now, anytime I use an AudioClip, I know it has play(), loop(), and stop()
An interface can be implemented in any number of classes, allowing each class to
share the programming interface without having to be fully aware of each other's implementation. If two very disparate classes implement the same interface, they can both respond to the same method calls (as defined by the interface), although what each class does in response to those methods calls may be very different.
Also, one can have multiple inheritence directly through the interface declaration:
So classes that implement MyFirstInterface must implement the methods defined in
both Interface1 and Interface2
If no extends is given for interfaces, they do not default to inheriting from Object.
(why not?)
if done this way, the variable obj is declared as the interface Storing, yet is assigned an instance of Image. This way, obj can be used to access the freezeDry method, and not any of the other aspects of the Image class.
Often a use of interfaces is to allow a callback: (The Java Handbook, page 139)
Don't forget that inheritance is not the only way that objects
are related. It may depend on the application as to what objects most
benefit by inheritance.
Sorting (CoreJava V1, Chapter 6). You want a general sorting routine that
would work on many different kinds of Java objects, so you write an
abstract class Sortable. Your choices:
Java chose to only allow single inheritence because mutilple inheritence makes compilers very complex (e.g., C++)
or very inefficient (e.g., Eiffel).
Implements:
The interface defined:
The class that implements it:
A class that uses it:
Note: a line elsewhere could have been this:
This is one of the key features of interfaces.
A concrete example use: in my CSCI 311 java class we wanted to time methods using different structures and algorithms. Since there is no way to directly point to methods in Java (no method pointers, only call by name for methods), we needed to work around this.One way to set this up was to have the classes to be timed implement a Timer. Another was to have each class to be timed have the same name (and use polymorphism). (Ultimately works the same, but an interface makes it clean...and this idea seemed to make the classes not as "OO reusable")
They had to do it from scratch; then the Core Java V1 book had it provided!And now Swing provides a Timer class in its package! (Not at all the same though Timer )
Suppose we start out with a Timer that is a thread
so we can have a bunch of things timed (it, of course, does not have to be
a thread).
We need the interface:
And a class that implements the Timed interface and has a method
that we want to time.
And an actual Timer class
This code explains only how an interface can be used to supply a notification
mechanism and hence, callback function. Of course there is more to threads
and to the actual Timer class. (For more on the Timer and callbacks, see
the 311 page )
The main use of interfaces is in the Delegation-event model of the AWT, which we see when working with event-handling.
For more, see CoreJavaV1, Chapter 6 and Java in a Nutshell
Also see the tutorial questions pageO'Reilly course 3: Lesson 6: Interfaces: Listeners and Adapters, Lesson 8:Interfaces: Listeners and Inheritance, Lesson 9: Interfaces and Callbacks