The old way of re-using existing programs

  • Suppose we need to write a Rectangle class and we found the Circle class that looks similar:

      public class Circle
      {
          private String color;
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle
      {
         
      
         
      
          
          
           We need to write this class                       
        
      
      
        
        
        
        
         
      
          
         
             
         
      }
      

Comment 1: I have simplified the example in the text book (uses fewer variables and methods)

The old way of re-using existing programs

  • The old way is to (1) copy the Circle class and then (2) make changes to the variables and methods:

      public class Circle
      {
          private String color;
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle
      {
          private String color;
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

Comment 1: I have simplified the example in the text book (uses fewer variables and methods)

An object oriented design example

  • Notice:   there are duplication of variable definitions and methods in the program:

      public class Circle
      {
          private String color;
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle
      {
          private String color;
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

Duplication makes program maintainance very difficult (when you make a change, you may need to change in many places)

Test program on Circle and Rectangle objects

 

public class myProg
{
    public static void main(String[] args)
    {
        Circle c1 = new Circle("red", 2);
        Circle c2 = new Circle("blue", 4);
        
        Rectangle r1 = new Rectangle("green", 2, 3);
        Rectangle r2 = new Rectangle("red", 1, 4);        

        System.out.println( c1.getColor() + " " + c1.getArea() );
        System.out.println( c2.getColor() + " " + c2.getArea() );
        System.out.println( r1.getColor() + " " + r1.getArea() );
        System.out.println( r2.getColor() + " " + r2.getArea() );

    }
}
  

DEMO: demo/04-inheritance/04-copy+paste

Let's see how we can define a suitable superclass for Circle and Rectangle that allow us to remove duplication !

An object oriented design example

  • We find the common (shared) properties and actions:

      public class Circle
      {
          private String color;
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle
      {
          private String color;
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

We define a super class with all the common members

An object oriented design example

  • We define a superclass that contains the common (shared) properties and actions in all classes (i.e.: Circle and Rectangle:

      public class GeometricObject
      {
          private String color;
      
          GeometricObject( ) { }          // Constructor 1
      
          GeometricObject( String col )   // Constructor 2
          {
              color = col;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
      
          public double getArea()  // Dummy method !!
          {
              return 0;  // Some default value
          }
      }
      

Comment:   some methods in the superclass may not have a useful method body --- it's OK.
                     We will learn later this it is very useful for the polymorphism mechanism !!

An object oriented design example

  • We change the Circle and Rectangle classes and make them extend the superclass GeometricObject:

      public class Circle extends GeometricObject
      {
          private String color;
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
          private String color;
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

The classes will inherit:   (1) color, (2) getColor(), (3) setColor() and (4) getArea()

An object oriented design example

  • The String color instance variable is the same as the superclass (GeometricObject) and we can remove the definition:

      public class Circle extends GeometricObject
      {
        
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
       
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
          public String getColor()          { return color; }
          public void   setColor(String c)  { color = c; }   
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

Note:   because color is private in GeometricObject, you must use its accessor/mutator methods to use the variable color

An object oriented design example

  • The getColor() and setColor() methods are the same as the superclass (GeometricObject) and we can remove the definition:

      public class Circle extends GeometricObject
      {
        
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
        
         
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea()
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
       
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
        
         
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea()       
          {
              return width*height;
          }
      }
      

Note:   because getColor()/getColor() is public in GeometricObject, you can use them directly !

An object oriented design example

  • The getArea() method is different from the GeometricObject, so we override it with a new getArea() in the subclass:

      public class Circle extends GeometricObject
      {
        
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
        
         
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea() // Overrides
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
       
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
        
         
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea() // Overrides       
          {
              return width*height;
          }
      }
      

Note:   because getColor()/getColor() is public in GeometricObject, you can use them directly !

An object oriented design example

  • There is one problem: (can you spot it ?)

      public class Circle extends GeometricObject
      {
        
      
          private double radius;
      
          Circle(String col, double r)
          {
              color = col; radius = r;
          }
      
        
         
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea() // Overrides
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
       
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              color = col; width = w; height = h;
          }
      
        
         
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea() // Overrides       
          {
              return width*height;
          }
      }
      

Problem:   color is private in GeometricObject and cannot be accessed directly

An object oriented design example

  • Replace all direct access to private members in superclass with calls to accessor/mutator methods:

      public class Circle extends GeometricObject
      {
        
      
          private double radius;
      
          Circle(String col, double r)
          {
              setColor(col); radius = r;
          }
      
        
         
          public double getRadius()         { return radius;}
          public void   setRadius(double r) { radius = r; }
      
          public double getArea() // Overrides
          {
              return 3.14159*radius*radius;
          }
      }
      
      
      
      
      public class Rectangle extends GeometricObject
      {
       
          private double width;
          private double height;
      
          Rectangle(String col, double w, double h)
          {
              setColor(col); width = w; height = h;
          }
      
        
         
          public double getWidth()          { return width;}
          public void   setWidth(double r)  { width = r; }
          public double getHeight()         { return height;}
          public void   setHeight(double r) { height = r; }
      
          public double getArea() // Overrides       
          {
              return width*height;
          }
      }
      

Notice:   now there is no duplication of variable defintions or methods !

Test program on Circle and Rectangle objects

 

public class myProg
{
    public static void main(String[] args)
    {
        Circle c1 = new Circle("red", 2);
        Circle c2 = new Circle("blue", 4);
        
        Rectangle r1 = new Rectangle("green", 2, 3);
        Rectangle r2 = new Rectangle("red", 1, 4);

        System.out.println( c1.getColor() + " " + c1.getArea() );
        System.out.println( c2.getColor() + " " + c2.getArea() );
        System.out.println( r1.getColor() + " " + r1.getArea() );
        System.out.println( r2.getColor() + " " + r2.getArea() );
    }
}
  

DEMO: demo/04-inheritance/05-OOD

This test program works like before !!!