Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Spring 2010 Today’s Topics Comments and/or Questions? abstract classes --- example with Account abstract methods Polymorphism Interfaces

3 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

4 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. To implement this stuff...

5 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 Cats do not. Data to be stored in Pet is: – Name – 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.

6 More on overriding methods 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.

7 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. Make the method abstract if the superclass doesn't need to be instantiable and if there is no obvious default behavior for the method in the superclass --- this passes the requirement of the behavior of the abstract method to any subclass that wants 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

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

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

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

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

12 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)‏

13 Polymorphism via Inheriance Pet 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.

14 Polymorphism via Inheriance Let'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.

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

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

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

18 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? The purpose of interfaces are to formally define the ways in which we can interact with a class (that implements the interface).

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

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

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

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

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

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

25 Interfaces Let's revisit the bubbleSort method that sorted ints and make it sort Comparables. Why?

26 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


Download ppt "CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann."

Similar presentations


Ads by Google