OOP Class9

esse quam videri
Revision as of 18:51, 9 November 2006 by Jeff (talk | contribs) (Delgates)
Jump to: navigation, search


Interfaces

Interfaces - implements or derives from

An interface is a class that lacks implementation. The only thing it contains are definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. Since classes in c# can only be derived from one other class ( in c++ it is possible to derive from many) interfaces are used for multiple inheritance. Like abstract classes you cannot create an instance of an interface on derive from it.

You cannot create an instance of an interface.

Interfaces cannot contain any implementation.

Interfaces are basically a contract between classes. When a class implements an interface it is promising to implement the propery and method signature of that interface. This helps with abstraction and encurages polymorphism.

In order to specify that a class implements an inteface you use a : after the class name:

<csharp>public class NewDog : IWalkable</csharp>

This means that the NewDog class implements the IWalkable interface.

Interfaces support multple inheritance.

<csharp>public class NewDog : IWalkable,IBark</csharp>

Now the NewDog Class promises to implement the IWalkable and the I Bark interface.


Phone interface example with single inhertance

phoneIFace.cs -source

Multiple interface inheritance - inherits IPhone, Cell, POTS phoneIFacePOTS.cs -source

Phone Interface UML

PhoneInterface.png

IComparable and Polymorphism

In order to allow build in Arry type like the ArrayList to be able to Sort and Reverse your classes need to implement the IComparable Interface. System.IComparable on MSDN

After your classes implement this interface it will allow for polymorphic method like Sort and Reverse to work.

Here is an exmaple of a dog array that will not sort

dogSimple.cs

This IComplare Interface has calls for a methpod called CompareTo. The CompareTo method shoudl retun an positive integerg if that the current object is grater that the object that is being compared. It should oreturn 0 is they are equal. And it should return a negative integer is the current object is less than the compared object.

The UML for IComparable
IComparable.png

The UML for a dog that implemets IComparable

DogIComparable.png


Now here is an example of a Dog class that implements the IComparable inteface.

dogICompare.cs

Other Resources on Interfaces

ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 4

ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 5


Web Forms

Examples of Web Forms

http://iam.colum.edu/oop/classsource/class9/aspForms.aspx aspForms.aspx - Source

todo examples of all the html elements and how they react to form submits

Persisting Data

POST and GET

an asps page posting to itself


Events in c#

Event handlers

OnClick

http://iam.colum.edu/oop/classsource/class9/events/events1.aspx events1.aspx - source

OnCommand

http://iam.colum.edu/oop/classsource/class9/events/events2.aspx events2.aspx - source

http://iam.colum.edu/oop/classsource/class9/events/events3.aspx events3.aspx - source

Home Work

Make an post an catch aspx page with a form of your very own that asks questions about one of your objects. Use regular html form and aspx events to demostrate you class.


Use the form values to create a new instance of your class.

Get and read Intro and Chapter 1 of Head Start Design Patterns