21
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

  • View
    221

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

CS 106Introduction to Computer Science I

04 / 21 / 2008

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Spring 2008

Today’s Topics• Comments and/or Questions?• polymorphism via inheritance• interfaces• polymorphism via interfaces

Page 3: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism• Polymorphism --- having many forms

• A polymorphic reference variable is one that can refer to different types of objects at different points in time

• An example from a text I used in the past (Lewis and Loftus):

obj.doIt();

• If obj is polymorphic --- meaning it can take on the value of different types of objects throughout the running of the program then it might be calling different versions of the doIt() method.

• What does that mean --- different versions of the doIt() method?

Page 4: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism• It means doIt() may be a method that is in multiple classes and

the actual method that gets executed depends on the type of the object.

• You can imagine that line of code:

obj.doIt();

may be in a loop. Then, if the obj reference changes and actually refers to different types of objects, the different versions of doIt() are called.

• This deals with the concept of binding.

Page 5: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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

Page 6: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism via Inheriance• It can go futher. If Pet inherits from LivingThing, then

this is allowed:LivingThing lt;

Pet a_pet;

Dog d = new Dog(“Max”);

a_pet = d;

lt = d;

• Here, the Dog reference is being stored in a reference (a_pet) of its immediate superclass (Pet) and also stored in a reference (lt) of a higher superclass (LivingThing.)

Page 7: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism via Inheriance• Below, some_pet is polymorphic because it can refer to a

Dog object or a Cat object (or a Pet object if Pet is not abstract.)

Pet some_pet = new Dog(“Max”);

System.out.println(some_pet.speak());

// Because there are subclasses of Pet (namely Dog

// and Cat) the some_pet reference can refer to different

// types of objects -> Java must know which speak()

// method to execute (the one in Dog, Cat or Pet)

Page 8: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism via InheriancePet some_pet = new Dog(“Max”);

some_pet = new Cat(“Kitty”);

System.out.println(some_pet.speak());

That's where the dynamic binding of variable to actual type comes into play.

Above, some_pet was a reference to a Dog then it became a reference to a Cat, so the speak() method is the Cat's speak method that it executes.

Page 9: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism via InherianceLet's add some code that stores an array of Pet

references, but each object actually stored in the Pet references may be a Dog, Cat or TalkingDog object.

Page 10: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Overriding methods• Polymorphism is possible via inheritance because of

overriding methods.

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

• A class containing an abstract method is not instantiable (can't create objects of this class) instead it is used as a superclass.

– If a subclass wants to be instantiable then the subclass that derives from the abstract superclass is required to implement (provide code for) any abstract methods in the superclass.

Page 11: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Overriding methods• speak() was an abstract method in the parent class Pet.• All subclasses of Pet were required to implement that method if the

subclasses were to be instantiable.• If speak() did not live in Pet, then we would still be allowed to

have speak() methods in all the subclasses but we would lose a couple of benefits:

– 1) nothing requires the signatures of the speak() methods in each subclass to be the same.

– 2) we won't be able to call the speak() method using a reference to a Pet.

Page 12: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Polymorphism• Being able to call the speak() method on a reference to a Pet ---

that's the key concept here.

• Only during runtime, during the time that the actual method call occurs does Java know what kind of object is referenced by a Pet reference. A Pet reference could refer to a Dog, Cat or TalkingDog object (because these classes all derive from Pet directly or indirectly.)

• Java then calls the appropriate speak() method based on which kind of object is actually calling speak() (i.e. a Dog, Cat or TalkingDog).

Page 13: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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

Page 14: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Interfaces• We can create our own interfaces (and we will later).

• We can also use interfaces in the Java API.

Page 15: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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 is meant to return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 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.

Page 16: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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?

Page 17: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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?

Page 18: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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.

Page 19: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

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?

}

Page 20: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Interfaces• Let's revisit the bubbleSort method that sorted ints and make it

sort Comparables.

• Why?

Page 21: CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann

Interfaces• Let's revisit the bubbleSort method that sorted an array of ints

and make it sort an array of Comparables.

• Why?– because it will be more flexible --- it will be able to sort an

array of objects of any class that implements the Comparable interface