What is an interface? Why is an interface useful?

Similar documents
We are on the GUI fast track path

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Chapter 9 Abstract Classes and Interfaces

CS171:Introduction to Computer Science II. Li Xiong

Chapter 14 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Motivations. Chapter 13 Abstract Classes and Interfaces

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Chapter 13 Abstract Classes and Interfaces

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Abstract Classes Interfaces

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Inheritance and Polymorphism

Inheritance, Polymorphism, and Interfaces

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS1150 Principles of Computer Science Objects and Classes

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Inheritance. Inheritance

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS-202 Introduction to Object Oriented Programming

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

25. Generic Programming

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Topic 7: Algebraic Data Types

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Use the scantron sheet to enter the answer to questions (pages 1-6)

Interfaces. James Brucker

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Inheritance (continued) Inheritance

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

CSCI 212 Second Midterm Exam Spring

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Introduction to Inheritance

Distributed Systems Recitation 1. Tamim Jabban

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Chapter 11 Inheritance and Polymorphism

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

ITI Introduction to Computing II

COMP 250. inheritance (cont.) interfaces abstract classes

Chapter 9 Inheritance

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

8. Polymorphism and Inheritance

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

ITI Introduction to Computing II

Reviewing OO Concepts

Chapter 15: How to Work with Interfaces and Generics

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Object-Oriented Programming in C# (VS 2015)

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Outline. More optimizations for our interpreter. Types for objects

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance and Interfaces

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java Comparable interface

Java Fundamentals (II)

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Methods Common to all Classes

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Reviewing OO Concepts

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Advanced Placement Computer Science. Inheritance and Polymorphism

Logistics. Final Exam on Friday at 3pm in CHEM 102

IUE Faculty of Engineering and Computer Sciences Spring Semester

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Super-Classes and sub-classes

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Distributed Systems Recitation 1. Tamim Jabban

Handout 7. Defining Classes part 1. Instance variables and instance methods.

INHERITANCE. Spring 2019

CMSC132 Summer 2018 Midterm 1. Solution

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Object-Oriented Programming in C# (VS 2012)

CMSC132 Summer 2018 Midterm 1

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Polymorphism and Inheritance

Transcription:

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

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 2

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 3

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

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 5

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"; 6 internal class Orange : Fruit public override string HowToEat() return "Orange: Make orange juice";

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 7 7 if ( circle1.compareto(circle2) == 0 ) // do something here... 7

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

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: < 9 9

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

Defining Classes to Implement IComparable Notation: The interface name and the method names are italicized. The dashed lines and hollow triangles are used to point to the interface. GeometricObject Rectangle ComparableRectangle «interface» IComparable +compareto(o: Object): int 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) ); 11 11

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; 12

Example: Implement Comparable Rectangles 1 2... ComparableRectangle cr1 = new ComparableRectangle(10, 20); ComparableRectangle cr2 = new ComparableRectangle(20, 10); ComparableRectangle cr3 = new ComparableRectangle( 4, 50); Rectangle r4 = new Rectangle(10, 20); 3 4 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 13 13 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).

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(); 14

Example: Implementing ICloneable & IComparable We will make a better Circle class by allowing it to implement the IClonable and IComparable interfaces. 1 public object Clone() return new Circle(Radius, Color, Filled); 2 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; 15

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. 16

Example: Overriding the Equals method public override bool Equals(Object otherobject) if (otherobject is Circle) else return this.radius == ((Circle)otherObject).Radius; 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 17