Design Patterns  «Prev  Next»

Three types of design Patterns

Polymorphism allows instances of different classes to be used as an instance of a common superclass[1]. For example, an EncryptionStream object might only have a reference to an abstract Cipher object.
The type of encryption the EncryptionStream performed could be adjusted by changing from a DESCipher to an RSACipher.
In cryptology, RSA is an algorithm for public-key cryptography. It was the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptography. RSA is widely used in electronic commerce protocols, and is believed to be secure given sufficiently long keys and the use of up-to-date implementations.

Real World Example of Polymorphism:

Example 1: A Teacher interacts with student. A Teacher interacts with his or her seniors.
Here teacher is an object but the attitude is different based on the situation.
Example 2: Person interacts with genetic offspring in house at the same time that person interacts with an empoloyee in the office.
Example 3: Your mobile phone, has one name of many forms:
  1. As phone
  2. As camera
  3. As mp3 player
  4. As radio

Polymorphism with classes

Polymorphism comes into the picture when a class inherits another class and both the base and the derived classes define methods with the same method signature (the same method name and method parameters). As discussed in the previous section, an object can also be referred to using a reference variable of its base class. In this case, depending upon the type of the object used to execute a method, the Java runtime executes the method defined in the base or derived class.
Let us examine polymorphism using the classes Employee, Programmer, and Manager, where the classes Programmer and Manager inherit the class Employee. Figure 6.21 shows a UML diagram depicting the relationships among these classes. Let us start with the Employee class, which is not quite sure about what must be done to start work on a project (execute method startProjectWork). Hence, the method startProjectWork is defined as an abstract method, and the class Employee is defined as an abstract class, as follows:
Figure 2-3 Relationships among the classes Employee, Programmer, and Manager
abstract class Employee {
 public void reachOffice() {
  System.out.println("reached office - Gurgaon, India");
 public abstract void startProjectWork();

The class Programmer extends the class Employee, which essentially means that it has access to the method reachOffice defined in Employee. Programmer must also implement the abstract method startProjectWork, inherited from Employee. How do you think a programmer will typically start work on a programming project? Most probably, he will define classes and unit test them. This behavior is contained in the definition of the class Programmer, which implements the method startProjectWork, as follows:
class Programmer extends Employee {
 public void startProjectWork() {
 private void defineClasses() { System.out.println("define classes"); }
 private void unitTestCode() { System.out.println("unit test code"); }

[1]superclass: A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors.