Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST311 / 602 Cleveland State University – Prof. Victor Matos

Similar presentations


Presentation on theme: "IST311 / 602 Cleveland State University – Prof. Victor Matos"— Presentation transcript:

1 IST311 / 602 Cleveland State University – Prof. Victor Matos
IST311 Interfaces IST311 / Cleveland State University – Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

2 What is an interface? Why is an interface useful?
An interface is a special C# class that contains only abstract methods and properties. Like abstract classes, they cannot be instantiated using new. The intent of an interface is to specify behavior for objects. For example, you can indicate that the objects are: IComparable ICloneable Fulfill any other user-define forms of behavior 2

3 UML Representation of Classes and Interfaces
C# classes use single inheritance. An interface is depicted with a relatively similar UML notation. A Match-Making app may want Client to extend person and implement the interface: LoveCompatible as well as Comparable A banking app may want its Client class to extend Person implement Comparable and ignore the LoveCompatible interface. 3

4 Define an Interface Example:
To distinguish an interface from a class, C# uses the following syntax to define an interface: interface InterfaceName { method signatures; } In an interface all methods are public abstract. For this reason, these modifiers should be omitted. Example: interface Edible { /** Describe how to eat */ String HowToEat(); } 4

5 Example: Using Interfaces
The IEdible interface specifies whether an object is edible and if so, it recommends the best way to eat the object. This behavior is accomplished by letting the class for the given object implement the IEdible interface. In the example, the classes Chicken and Fruit implement the IEdible interface, however DeadlySpider does not. interface IEdible { // Describes best way to eat it object String HowToEat(); } 5

6 Example: Using Interfaces
public static void Main(string[] args) { object[] basket = new object[] {new DeadlySpider(), new Chicken(), new Apple() }; foreach (Object food in basket) if (food is IEdible) { Console.WriteLine(((IEdible)food).HowToEat()); } } internal class Animal { // Data fields, constructors, and methods omitted here } internal class Chicken : Animal, IEdible public virtual string HowToEat() { return "Chicken: Fry it"; } internal class DeadlySpider : Animal { } internal abstract class Fruit : IEdible public virtual string HowToEat() { return "Peal it first"; } internal class Apple : Fruit public override string HowToEat() { return "Apple: Make apple cider"; } internal class Orange : Fruit public override string HowToEat() {return "Orange: Make orange juice";}

7 Omitting Modifiers in Interfaces
7

8 Example: The IComparable Interface
Assume you need to compare two Circle objects. You could use a C# fragment such as: if ( circle1.Area == circle2.Area ){ // do something here... } However to compare two Client objects you may say if ( client1.GetId() == client2.GetId() ){ // do something here... } Previous ideas are OK, but perhaps a more general way of comparing objects is needed. For example if ( circle1.CompareTo(circle2) == 0 ){ // do something here... } 8 8

9 The IComparable Interface
When you try to contrast two arbitrary objects, they should be comparable. The C# interface supporting object comparisons is defined as follows: // interface for comparing objects interface IComparable { int CompareTo(Object o); } Example: int result = firstObject.CompareTo(secondObject); 0 if firstObject == secondObject result > 0 if firstObject > secondObject < 0 if firstObject < secondObject 9

10 Why the IComparable Interface ?
The fundamental goal of the IComparable interface is to decide how to arrange two objects from an arbitrary class according to an intuitive notion of a ‘natural ordering’, sequencing, or arrangement of those kind of elements. For instance, consider the following sets of objects Integers: -n < … <-2 < -1 < 0 < 1 < 2 < … n < n + 1 Strings: Unicode (alphabetic) collating sequence Dates: Jan < Feb < … < Dec Rectangles: < 10

11 String and Date Classes are Comparable
Many classes (e.g., String and DateTime) in the C# library implement IComparable to define a natural order for the objects. All the following expressions are true new String() is String new String() is IComparable new DateTime is DateTime new DateTime is IComparable 11

12 Defining Classes to Implement IComparable
The Rectangle class does not provide a method to compare rectangles. However, you can define a new subclass of the Rectangle class that implements Comparable. The instances of this new class are therefore comparable. ComparableRectangle rectangle1 = new ComparableRectangle(4, 5); ComparableRectangle rectangle2 = new ComparableRectangle(3, 6); Console.WriteLine( rectangle1.CompareTo(rectangle2) ); 12 12

13 Example: Implement Comparable Rectangles
public class ComparableRectangle : Rectangle, IComparable { // Construct a ComparableRectangle with specified properties public ComparableRectangle(double width, double height) : base(width, height) {} // Implement the CompareTo method defined in IComparable public int CompareTo(object obj) ComparableRectangle otherRectangle = (ComparableRectangle)obj; if (this.Area() > otherRectangle.Area()) return 1; } else if (this.Area() < otherRectangle.Area()) return -1; else return 0; 13

14 Example: Implement Comparable Rectangles
... ComparableRectangle cr1 = new ComparableRectangle(10, 20); ComparableRectangle cr2 = new ComparableRectangle(20, 10); ComparableRectangle cr3 = new ComparableRectangle( 4, 50); Rectangle r4 = new Rectangle(10, 20); int c12 = cr1.CompareTo(cr2); // c12 is 0 int c13 = cr1.CompareTo(cr3); // c13 is 0 int c23 = cr2.CompareTo(cr3); // c23 is 0 int c14 = cr1.CompareTo(r4); // produces run-time error int c41 = r4.CompareTo(cr1); // syntax error 1 2 3 4 Observation: In this example the ComparableRectangles cr1, cr2, and cr3 are equal (they all have different width and height but they have a similar area -which is the criteria used for establishing the comparison among them). 14 14

15 The ICloneable Interface
A class that implements the ICLoneable interface is designated cloneable, and its objects can be duplicated using the Clone() method. interface ICloneable { Object Clone(); }

16 Example: Implementing ICloneable & IComparable
We will make a better Circle class by allowing it to implement the IClonable and IComparable interfaces. public object Clone() { return new Circle(Radius, Color, Filled); } public int CompareTo(object obj) Circle otherRectangle = (Circle)obj; if (this.Area() > otherRectangle.Area()) return 1; else if (this.Area() < otherRectangle.Area()) return -1; else return 0; 1 2 16

17 Example: The ICloneable Interface
static void Main(string[] args) { Circle c1 = new Circle(2, "Pink", true); Circle c2 = (Circle)c1.Clone(); Console.WriteLine(" c1==c2 is " + (c1 == c2)); //prints false Console.WriteLine(" c1.Equals(c2) is " + c1.Equals(c2)); //prints false Console.WriteLine(" c1.CompareTo(c2) is " + c1.CompareTo(c2)); //prints 0 } Circles c1 and c2 are two different objects with identical content. The Clone method copies each field from the original object to the target object. 17

18 Example: Overriding the Equals method
public override bool Equals(Object otherObject) { if (otherObject is Circle) return this.Radius == ((Circle)otherObject).Radius; else return false; } You may override the Equals method of the Circle class to return true each time the contents of the two compared objects are the same. After this change is made the test code in previous page produces: r1==r2 is false r1.equals(r2) is true r1.CompareTo(r2) is 0 18


Download ppt "IST311 / 602 Cleveland State University – Prof. Victor Matos"

Similar presentations


Ads by Google