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

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Polymorphism & Interfaces
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 27 / 2006 Instructor: Michael Eckmann.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Types of Programming Languages
Programming Language Concepts (CIS 635)
Object Oriented Programming
Designing for Inheritance
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? polymorphism via inheritance interfaces polymorphism via interfaces

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?

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.

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

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

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

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.

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.

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.

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.

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

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

Interfaces We can create our own interfaces (and we will 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( 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.

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?

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?

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.

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

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