90 likes | 278 Views
Typing & O-O. Type == Class. Two Categories of Type. reference type -- store only the object’s address. simple (primitive) type -- for storing small data values. related issues: deep/shallow equality, alias vs. cloning. Other options.
E N D
Typing & O-O Type == Class Two Categories of Type reference type -- store only the object’s address simple (primitive) type -- for storing small data values related issues: deep/shallow equality, alias vs. cloning Other options Wrapper classes(Java, C# calls this boxing) Expanded objects(Eiffel)
The Basics of O-O What is a message? Type Checking Techniques (This should sound familiar.) Static type checking - compile time Dynamic type checking - run time
Inheritance Eiffel Example classBASE_CLASS feature {ANY} foo is … poo is … doo is … end classDERIVED_CLASSinherit BASE_CLASS rename poo as too redefine doo export {NONE} foo end feature {ANY} doo is … end
Multiple Inheritance classMULTI_CLASSinherit BASE_CLASS rename foo as footoo export {NONE} footoo end ANOTHER_BASE feature {ANY} … end classBASE_CLASS feature {ANY} foo is … poo is … doo is … end classANOTHER_BASE feature {ANY} foo is … moo is … end
Multiple Inheritance classMULTI_CLASSinherit BASE_CLASS rename foo as footoo export {NONE} footoo end ANOTHER_BASE redefine moo end ANOTHER_BASE rename mooas parent_moo export {NONE} parent_moo end feature {ANY} moo is do parent_moo … end end classBASE_CLASS feature {ANY} foo is … poo is … doo is … end classANOTHER_BASE feature {ANY} foo is … moo is … end
Polymorphism ClosedFigure Polygon Ellipse Quadrilateral Triangle Pentagon Circle Square ClosedFigure fig; fig = somePolygon; … fig = someCircle; … fig = someSquare 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. 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
Co/Contra variance Example (Java syntax) public class Parent { public void foo(ParentParm parm) { … } } public class Child extends Parent { public void foo(ChildParm p) { … } } When does the child’s version of foo override (redefine) the parent’s? 1) only if ParentParm and ChildParm are the same class 2) only if ChildParm is a descendant (subclass) of ParentParm 3) only if ParentParmis 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.
Genericity A generic (parameterized) class ___________________________________ Example (Eiffel syntax). classBAG[ElementType] creation make feature {ANY} count : INTEGER -- number of elements in the bag make is … item : ElementType is … insert(e : ElementType) is … end Declaration of a BAG object (perhaps in some other class) myBag : BAG[STRING]; Typical method call myBag.insert( “xyz” );
Genericity Java (proposed) syntax. public classBag<ElementType> { int count ; //number of elements in the bag public Bag (...) { ... } public ElementTypeitem() { ... } public void insert(ElementType e) { .... } } Declaration of a Bag object (perhaps in some other class) myBag : Bag<String>; Instantiation of a BAG object. myBag = new Bag<String>(...); Typical method call myBag.insert( “xyz” ); Other possibilities... public classPair< T1, T2 implements Comparable > {