CIT-73 Home http://www.c-jump.com/CIT73/CIT73syllabus.htm

Chapter 1, Encapsulation


  1. Week 3, Chapter 1, Encapsulation and Inheritance
  2. Class interface
  3. Class implementation
  4. Inheritance
  5. Superclasses and subclasses
  6. Is-a relationship
  7. Polymorphism
  8. Main Driver
  9. Composition and Encapsulation
  10. Inheritance
  11. Animation: Inheritance and Polymorphism
  12. Week 3 Reading: pages 17-27

1. Week 3, Chapter 1, Encapsulation and Inheritance



  • Object

      Ford Vicky

  • Public interface

      public interface

  • Encapsulated parts

      encapsulation

2. Class interface


  • Interface is a fundamental means of communication between objects.

  • Each class design specifies its public interface or a combination of interfaces.

  • All attributes should be declared private...

    • ...thus, attributes are not part of the interface.

    interface

3. Class implementation


  • Parts of the class not included in its public interface belong to implementation:

    • private attrbutes

    • private Java methods (private C++ member functions.)

  • Compiler prevents users from gaining access to the class implementation.

  • Implementation is hidden from the user.

  • Java example of the interface/implementation:

    
    public class IntSquare {
        // private attribute
        private int squareValue;
    
        // public interface
        public int getSquare( int value ) {
            SquareValue = calculateSquare( value );
            return squareValue;
        }
    
        // private implementation
        private int calculateSquare( int value ) {
            return value * value;
        }
    }
    
    
  • Corresponding UML diagram:

     

    IntSquare
    -squareValue:int
    +getSquare:int
    -calculateSquare:int

  • If implementation changes, the interface remains the same.

4. Inheritance


  • Inheritance (generalization) represents commonality among concepts.

  • Commonality must be actively sought.

  • Search for generalization and classification of concepts are high-level activities that yield lasting results.

  • Mammal hierarchy example:

      mammal hierarchy

5. Superclasses and subclasses


  • The superclass, or parent class, or base class are common terms that refer to a class that contains common attributes and behaviors of a concept.

  • Naturally, any class that inherits from the superclass is considered a subclass, or child class, or derived class.

  • Deriving new classes from an existing class abstracts the base class.

  • Derived classes have the data and functions of the base class.

  • Inheritance tree can grow quite large:

      Mammal UML diagram

6. Is-a relationship


  • Is-a relationship suggests that both circle and square are shapes.

  • Note that Circle, Star, and Square can independently draw themselves.

  • Such architecture supports polymorphism, or polymorphic behavior of a shape.

  • The shape hierarchy:

      shape hierarchy

7. Polymorphism


  • Polymorphism stands for uniform interface .

  • Each derived class inherits the base interface.

  • Each derived class provides its own implementation of each method in the interface.

  • When uniform message (like draw) is sent to an object, it will respond in a distinct way.


  • Shape Java class:

    
    public abstract class Shape {
        private double area;
        public abstract double getArea();
    }
    
    
  • Shape UML diagram:

      Shape UML diagram


  • Rectangle Java class:

    
    public class Rectangle extends Shape{
        double length;
        double width;
    
        public Rectangle( double l, double w ) {
            length = l;
            width = w;
        }
    
        public double getArea() {
            area = length * width;
            return ( area );
        }
    }
    
    
  • Circle Java class:

    
    public class Circle extends Shape {
        double radius;
    
        public Circle( double r ) {
            radius = r;
        }
    
        public double getArea() {
            area = 3.14 * ( radius * radius );
            return ( area );
        }
    }
    
    

8. Main Driver


9. Composition and Encapsulation


10. Inheritance


11. Animation: Inheritance and Polymorphism


12. Week 3 Reading: pages 17-27