CIS-260 Home http://www.c-jump.com/bcc/

Inheritance and Composition


  1. Inheritance and Composition
  2. Inheritance
  3. Inheritance, continued
  4. Inheritance, continued
  5. Composition and UML Association
  6. Composition and UML Aggregation
  7. Encapsulation revisited
  8. Polymorphism
  9. Polymorphic Operations
  10. Polymorphism Example
  11. Animation: Object Oriented Paradigm

1. Inheritance and Composition


  • Both are forms of object reuse

  • Both have weaknesses and strengths

  • Let's start with the parent class.

  • For example, here is a class diagram for the Dog class:

    Dog class

2. Inheritance


  • Inheritance advantage:

    • if base class operations change, no other changes would be required in the derived classes.

  • The idea cf inheritance is to go from the general to the specific by factoring out commonality.

  • Sometimes factoring out is a multi-step process:

    • The GoldenRetriever class inherits from the Dog class

    Dog class

3. Inheritance, continued


  • The LhasaApso class also inherits from the Dog class:

    LhasaApso class

4. Inheritance, continued


  • After discovering that not all dogs bark, more changes take place.

  • The final Dog class hierarchy becomes:

    final class hierarchy

5. Composition and UML Association


  • Association is a relationship between objects describing availability of instances to locate each other.

  • Associations are the glue that holds together an object model.

  • UML associations can

    • have names

    • be ordered

    • specify multiplicity (cardinality, or size of a collection)

    • spell object roles

    • provide other attributes.

    association

6. Composition and UML Aggregation


  • Aggregation is a form of association that specifies a whole-part relationshipas found in compositions of objects.

  • If the aggregation is a composition, then the diamond is filled.

  • Composition usually means physical containment.

  • Aggregation notation:

      aggregation notation

7. Encapsulation revisited


8. Polymorphism


  • Polymorphism means that if operation is invoked on a parent class, the implementation is supplied by a descendant class.

  • Thus, an operation is said to be polymorphic when its implementation is supplied in a descendant class.

  • If operation implementation is supplied by the object on which tee operation was invoked, then the operations is said to be concrete.

  • Implementation of print operation is supplied by each of the descendant classes:

      printer hierarchy

9. Polymorphic Operations


  • Base class may have many possible children, each of which implements its own variation of an operation

  • A polymorphic operation can be declared without an implementation in a parent class with the intent that an implementation must be supplied by each descendant class.

  • Such an incomplete operation is abstract (shown by italicizing its name).

  • Polymorphic operations are enabled by generalization:

      generalization notation

10. Polymorphism Example


  • Practical use of polymorphism - array of shape-derived objects, Circles, Rectangles, and Triangles:

    
    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 Triangle extends Shape {
        public void draw() {
            System.out.println("I am drawing a Triangle");
        }
    }
    
    
  • 
    public class TestShapeArray {
        public static void main( String args [] ) {
            Shape [] myshapes = new Shape [4];
            myshapes [0] = new Circle();
            myshapes [1] = new Rectangle();
            myshapes [2] = new Circle();
            myshapes [3] = new Triangle();
            System.out.println(myshapes[0].draw());
            System.out.println(myshapes[1].draw());
            System.out.println(myshapes[2].draw());
            System.out.println(myshapes[3].draw());	
        } // end of main
    } // end of TestShapeArray 
    
    
  • Test Results:

        I am drawing a Circle
        I am drawing a Rectangle
        I am drawing a Circle
        I am drawing a Triangle   
    

11. Animation: Object Oriented Paradigm