October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Responsibility

  • May 24, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Now, take a look at some code. Imagine that there are four classes: the abstract Shape class, and concrete Circle, Rectangle, and Star classes. Here is the code:

public abstract class Shape{
   public abstract void draw();
}
public class Circle extends Shape{
   public void draw() {
      System.out.println("I am drawing a Circle");
   }
}
public class Rectangle extends Shape{
   public void draw() {
      System.out.println("I am drawing a Rectangle");
   }
}
public class Star extends Shape{
   public void draw() {
      System.out.println("I am drawing a Star");
   }
}

Notice that there is only one method for each class: draw( ). Here is the important point regarding polymorphism and an object being responsible for itself: The concrete classes themselves have responsibility for the drawing function. The Shape class does not provide the code for drawing; the Circle, Rectangle, and Star classes do this for themselves. Here is some code to prove it:

public class TestShape {
   public static void main(String args[]) {
      Circle circle = new Circle();
      Rectangle rectangle = new Rectangle();
      Star star = new Star();
      circle.draw();
      rectangle.draw();
      star.draw();
   }
}

The test application TestShape creates three classes: Circle, Rectangle, and Star. To actually draw these classes, TestShape simply asks the individual classes to draw themselves:

circle.draw();
rectangle.draw();
star.draw();

When you execute TestShape, you get the following results:



Click here for a larger image.

Figure 4: Running the TestShape Application

This is polymorphism at work. What would happen if you wanted to create a new shape, say Triangle? Simply write the class, compile it, test it, and use it. The base class Shape does not have to change—nor does any other code:

public class Triangle extends Shape{
   public void draw() {
      System.out.println("I am drawing a Triangle");
   }
}

A message can now be sent to Triangle. And even though Shape does not know how to draw a triangle, the Triangle class does:

public class TestShape {
   public static void main(String args[]) {
      Circle circle = new Circle();
      Rectangle rectangle = new Rectangle();
      Star star = new Star();
      Triangle triangle = new Triangle ();
      circle.draw();
      rectangle.draw();
      star.draw();
      triangle.draw();
   }
}



Click here for a larger image.

Figure 5: Adding the Triangle

To see the real power of polymorphism, you can actually pass the shape to a method that has absolutely no idea what shape is coming—just like with the drawing application example that you started out with where the mouse doesn't know the exact shape that it is supposed to draw. Note that in the following code, the application is calling a method called drawMe( ) that requires a single parameter that is defined as Shape. Thus, any valid Shape (actually, any class that inherits from Shape) can be passed to this method.

public class TestShape {
   public static void main(String args[]) {
      Circle circle = new Circle();
      Rectangle rectangle = new Rectangle();
      Star star = new Star();
      drawMe(circle);
      drawMe(rectangle);
      drawMe(star);
   }
   static void drawMe(Shape s) {
      s.draw();
   }
}

In this case, a circle, rectangle, and star object can be passed to the drawMe() method that can handle any valid Shape—even one you add later.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel