Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 106 Introduction to Computer Science I 04 / 16 / 2014 Instructor: Michael Eckmann Today’s Topics • Comments and/or Questions? • Dynamic binding • Interfaces – Comparable interface – Polymorphism via interfaces Michael Eckmann - Skidmore College - CS 106 - Spring 2014 Binding • Binding of a variable to a type usually occurs at compile-time. This means that when Java compiles the source code, it can figure out the exact type (class) associated with each variable name in the code at the time the code is compiled. • In the case of polymorphism, this binding of a variable to a type can only be done at runtime. Why? • This is called late binding or dynamic binding. Java needs to determine the ACTUAL type of the object being referred to by the reference. Pet p; // some code here to assign an object to p p.speak(); Recap • Inheritance (super/sub, parent/child) use extends keyword. • Abstract vs. Instantiable • Overriding methods • Polymorphism & Dynamic method binding – e.g. when have an array of Pets, each reference is a polymorphic reference either a Dog, TalkingDog or Cat and can call speak() directly on the Pet reference and the Java runtime system is able to call the correct speak() method based on the actual type of the object that the Pet reference refers to. This choosing the correct speak() method to call at runtime is dynamic method binding. Overloaded methods • Overloaded methods are those that have the same name but different numbers or types of parameters. • It has nothing to do with the super / subclass (parent / child) relationships that we've been talking about. • Does anyone remember when we used Overloaded methods? Interfaces • An interface is a collection of constants and abstract methods. – An abstract method is one that is defined by a header (signature) but no body. An abstract method, when defined in an interface, does not provide an implementation. • An interface can never be instantiated. What does that mean again? Interfaces • The purpose of interfaces are to formally define the ways in which we can interact with a class (that implements the interface). • A class is said to implement an interface if it does so with the correct syntax and provides definitions for each of the abstract methods in the interface. • A class can implement multiple interfaces, but may only extend one class directly. Interfaces • We can create our own interfaces (and we may do this later). • We can also use interfaces in the Java API. Interfaces • There are many interfaces in the Java API available for us to use. • One of these is the interface Comparable • It contains one abstract method: int compareTo( objectToBeCompared ) It is meant to return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the objectToBeCompared. Negative means <, Positive means > and 0 means equal. It is up to a class that implements the Comparable interface to determine what it means for its objects to be <, > or = to each other. That is specified in the implementation of the method. Interfaces • Example use of Comparable with this method: int compareTo( objectToBeCompared ) • Let's edit our Card class to implement Comparable. • Will Card need a compareTo method? Interfaces • Example use of Comparable with this method: int compareTo( objectToBeCompared ) • Let's edit our Card class to implement Comparable. • Will Card need a compareTo method? – Yes, but we already wrote it to those exact specifications Interfaces • Let's revisit the bubbleSort method that sorted ints and make it sort Comparables. • Why? Interfaces • Let's revisit the bubbleSort method that sorted an array of ints and make it sort an array of Comparables. Because it will be more flexible --- it will be able to sort an array of objects of any class that implements the Comparable interface • It can sort an array of Cards • It can sort an array of Employees – Assuming there is an Employee class and it implements the Comparable interface • It can sort an array of String (b/c String implements Comparable) • It can sort an array of any class – As long as that class implements the Comparable interface Interfaces • We know String has a compareTo method. • It implements the Comparable interface. Let's look at String in the Java API.