Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? another example of references and objects overriding the equals method from class Object in one of the Account classes Abstract class –abstract method –overriding abstract methods –example class hierarchy with an abstract superclass Interfaces –Comparable –example usage in Card –sorting using Comparables

3 References Let's say we have two card references: Card c1, c2; Let's instantiate c1: c1 = new Card(9,3); // 9 of Hearts Let's instantiate c2: c2 = new Card(4,2); // 4 of Clubs Now, c1 and c2 each refer to seperate objects. If we assign reference c1 to c2: c2 = c1; // assign reference c1 to c2 c2 now refers to the object that c1 referred to (still refers to.) What happened to the object that c2 referred to (4 of clubs)?

4 Overriding methods Recall from last time ---- A subclass can override a superclass's method by providing a definition for a method that exists in the superclass with the same name and number and type of parameters.

5 equals method In class Object, the header for the equals method is this: public boolean equals(Object obj) If we want to override this method, we must make sure our equals method has the SAME signature. That means equals will need to be defined to take in a parameter of type Object. Any subclass of Object can be passed in to the call of the equals method.

6 Account Program Let's override equals in class Card – boolean equals(Object o)

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

8 Abstract classes – Can never be instantiated – Can contain both abstract methods and actual (non-abstract) methods – Can contain instance variables as well as constants If a class contains any abstract methods then it MUST BE an abstract class But an abstract class is not required to have abstract methods Abstract classes are different from interfaces which we will see next.

9 More on overriding methods Suppose we have a class Pet, Dog, and Cat. We provide a speak() method in Pet but with no body. Then, any class that inherits from Pet, must implement this method. Further, let's assume we have a class TalkingDog which doesn't bark when he speaks, instead he speaks English.

10 Class Pet, Dog and Cat Let's suppose all Pets have names, breeds and make sounds, and know how to sleep. Let's also suppose that different Dogs have different skills, but different Cats do not. Data to be stored in Pet is: – Name – Type (cat, dog, etc.) – Breed Additional data to be stored in Dog is: – Skill No additional data is stored in Cat. Let's set up the classes with these data and the relationships among the classes.

11 Pet, Cat, Dog,... So we have classes Pet (abstract), Dog, and Cat. We can provide a speak() method in Pet with no body (and we make it abstract). Then, any class that inherits from Pet, must implement this method. We can provide an actual method sleeps() in Pet that is not abstract. And the class TalkingDog which doesn't bark when he speaks, instead he speaks English. So, Dog and Cat inherit from Pet. TalkingDog inherits from Dog. Let's implement this stuff (instantiate objects of Dog, Cat, and TalkingDog) Note: because Pet contains an abstract method, the class is not instantiable --- the class itself must be declared as abstract.

12 abstract methods The reason we create an abstract method (with an empty method body) in the superclass is so that this guarantees that all subclasses must provide this method if they are to be instantiable. Notice: –a subclass of Pet had to include code for the speak() method –we couldn't instantiate an object of type Pet because it is abstract

13 Interfaces An interface is a collection of constants and abstract methods. – An abstract method is one that is defined by a header 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? The purpose of interfaces are to formally define the ways in which we can interact with a class (that implements the interface).

14 Interfaces We can create our own interfaces (and we will later). We can also use interfaces in the Java API.

15 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( object_to_be_compared ) – It returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Negative 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.

16 Interfaces Example use of Comparable int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method?

17 Interfaces Example use of Comparable int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method? – Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface. What might our compareTo method do in the Card class?

18 Interfaces int compareTo( object_to_be_compared ) Let's edit our Card class to implement Comparable. Will we have to add a compareTo method? – Yes! If the class implements Comparable it must provide implementation of ALL the abstract methods of the interface. What might our compareTo method do in the Card class? – It should return a negative # if the “calling” Card object is less than the Card object that is passed in as a parameter. – It should return 0 if the Card objects are equal – Otherwise return a positive number.

19 Interfaces The Card class has rank and suit as instance variables. Our compareTo method will look like: public int compareTo(Object o) { // how do we refer to the values // associated with the “calling” Card object? // how do we refer to the values // associated with the parameter object? }

20 Interfaces Let's create another class that implements Comparable. How about a class to store contact information for someone. Let's store a person's first name, last name, and phone number. Let's create an array of Contacts and then sort that array. –We can start from the bubbleSort method which sorts an array of ints, but we'll make it sort an array of Comparables.


Download ppt "CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google