IST311 / 602 Cleveland State University – Prof. Victor Matos

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Chapter 11 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Chapter Goals To be able to declare and use interface types
Chapter 11 Inheritance and Polymorphism
03/10/14 Inheritance-2.
Chapter 11 Object-Oriented Design
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 8 Classes and Objects
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 13 Abstract Classes and Interfaces
CIS3023: Programming Fundamentals for CIS Majors II
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Inheritance and Polymorphism
Lecture 4: Interface Design
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Chapter 12 Abstract Classes and Interfaces
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

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

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

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

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

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

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";}

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

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

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

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) ); 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 ... 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). 13 13

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

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 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) 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 17