Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes.

Similar presentations


Presentation on theme: "Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes."— Presentation transcript:

1 Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes (Java, C# calls this boxing) Expanded objects (Eiffel) Two Categories of Type  related issues: deep/shallow equality, alias vs. cloning

2 What is a message? Static type checking - compile time Type Checking Techniques (This should sound familiar.) Dynamic type checking - run time

3 Eiffel Example class BASE_CLASS feature {ANY} foo is … poo is … doo is … end class DERIVED_CLASS inherit BASE_CLASS rename poo as too redefine doo export {NONE} foo end feature {ANY} doo is … end

4 class ANOTHER_BASE feature {ANY} foo is … moo is … end class MULTI_CLASS inherit BASE_CLASS rename foo as footoo export {NONE} footoo end ANOTHER_BASE feature {ANY} … end class BASE_CLASS feature {ANY} foo is … poo is … doo is … end

5 class ANOTHER_BASE feature {ANY} foo is … moo is … end class MULTI_CLASS inherit BASE_CLASS rename foo as footoo export {NONE} footoo end ANOTHER_BASE redefine moo end ANOTHER_BASE rename moo as parent_moo export {NONE} parent_moo end feature {ANY} moo is do parent_moo … end class BASE_CLASS feature {ANY} foo is … poo is … doo is … end

6 Note that a variable can be assigned (bound to) different kinds of objects. Subtype polymorphism means that a name can refer to differing types of objects so long as these objects are descendant (subtype) classes of the named class. ClosedFigure EllipsePolygon Square QuadrilateralPentagonTriangleCircle ClosedFigure fig; fig = somePolygon; … fig = someCircle; … fig = someSquare Dynamic dispatch means that the actual method to be executed (dispatched) is not determined until run-time. Suppose every class contains a parameterless method called calculatePerimeter

7 Example (Java syntax) public class Parent { public void foo( ParentParm parm) { … } When does the child’s version of foo override (redefine) the parent’s ? public class Child extends Parent { public void foo( ChildParm p) { … } 1) only if ParentParm and ChildParm are the same class 2) only if ChildParm is a descendant (subclass) of ParentParm 3) only if ParentParm is a descendant (subclass) of ChildParm 4) never (no overriding permitted) C++ permits both covariance & contravariance for concrete virtual methods, but invariance for pure virtual (deferred/abstract) methods.

8 A generic (parameterized) class ___________________________________ class BAG [ElementType] creation make feature {ANY} count : INTEGER -- number of elements in the bag make is … item : ElementType is … insert(e : ElementType) is … end myBag : BAG[STRING]; Example (Eiffel syntax). Declaration of a BAG object (perhaps in some other class) Typical method call myBag.insert( “xyz” );

9 public class Bag { int count ; //number of elements in the bag public Bag (...) {... } public ElementType item() {... } public void insert(ElementType e) {.... } } myBag : Bag ; Java (proposed) syntax. Declaration of a Bag object (perhaps in some other class) Typical method call myBag.insert( “xyz” ); myBag = new Bag (...); Instantiation of a BAG object. Other possibilities... public class Pair {


Download ppt "Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes."

Similar presentations


Ads by Google