Typing & O-O
Download
1 / 9

Typing & O-O - PowerPoint PPT Presentation


  • 156 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Typing & O-O' - kirk


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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 > {


ad