Difference between revisions of "OOP Class8"

esse quam videri
Jump to: navigation, search
(Interfaces)
Line 13: Line 13:
 
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.
 
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.
  
Phone interface example
+
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
  
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/phoneIFace.cs phoneIFace.cs] -source
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/phoneIFace.cs phoneIFace.cs] -source
Line 19: Line 32:
 
Multiple interface inheritance - inherits IPhone, Cell, POTS
 
Multiple interface inheritance - inherits IPhone, Cell, POTS
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/phoneIFacePOTS.cs phoneIFacePOTS.cs] -source  
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/phoneIFacePOTS.cs phoneIFacePOTS.cs] -source  
 +
 +
===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.
 +
[http://msdn2.microsoft.com/en-US/library/system.icomparable.aspx 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
 +
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/DogCompare/dogSimple.cs dogSimple.cs]
 +
 +
Now here is an example of a Dog class that implements the IComparable inteface.
 +
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class8/DogCompare/dogICompare.cs dogICompare.cs]
 +
 +
 +
 +
 +
===Other Resources on Interfaces===
  
 
[http://ia300218.us.archive.org/2/items/arsdigitac04n04/Lecture_04.html ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 4]
 
[http://ia300218.us.archive.org/2/items/arsdigitac04n04/Lecture_04.html ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 4]

Revision as of 16:42, 15 March 2006


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

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

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

Anatomy of an aspx page

Page Direcive

Console applications start executing in the main method

<csharp>public static void Main() {}</csharp>

The .Net Framework can also execute on the web. Rather than having a Main method a web page starts it's execution with a method called Page_Load

<csharp>public Page_Load { }</csharp>

There are actually several method that are exeuted in an aspx page.



Dogs on the web