只问耕耘
分类: Java
2010-01-14 11:41:18
I covered method overloading as one form of polymorphism (compile-time polymorphism) in a previous lesson.
Method overriding and class inheritance
I discussed runtime polymorphism implemented through method overriding and class inheritance in more than one previous lesson.
Using the Java interface
In this and the previous lesson, I am explaining runtime polymorphism as implemented using method overriding and the Java interface.
A very important concept
In my opinion, this is one of the most important concepts in Java OOP, and the one that seems to give students the greatest amount of difficulty. Therefore, I am trying to take it slow and easy. As usual, I am illustrating the concept using sample programs.
A skeleton program
In the previous lesson, I presented a simple skeleton program that illustrated many of the important aspects of polymorphic behavior based on the Java interface.
Multiple inheritance and the cardinal rule
I explained how the implementation of interfaces in Java is similar to multiple inheritance.
I explained the cardinal rule of interface implementation.
A new relationship
I explained that objects instantiated from classes that implement the same interface have a new relationship that goes beyond the relationship imposed by the standard class hierarchy.
One object, many types
I explained that due to the combination of the class hierarchy and the fact that a class can implement many different interfaces, a single object in Java can be treated as many different types. However, for any given type, there are restrictions on the methods that can be invoked on the object.
Many classes, one type
I explained that because different classes can implement the same interface, objects instantiated from different classes can be treated as a common interface type.
Interfaces are critical to Java programming
I suggested that there is little if anything useful that can be done in Java without understanding and using interfaces.
In support of this suggestion, I discussed several real-world examples of the use of the Java interface, including the Delegation Event Model and Remote Method Invocation.
Another sample program
In this lesson, I will present another sample program that will take you deeper into the world of polymorphism as implemented using the Java interface.
The sample program that I will discuss in this lesson will illustrate (in a very basic form) some of the things that you can do with interfaces, along with some of the things that you cannot do with interfaces. In order to write programs that do something worthwhile, you will need to extend the concepts illustrated by this sample program into real-world requirements.