CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



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

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Polymorphism & Interfaces
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Interfaces & Polymorphism part 2:
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.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 151: Object-Oriented Design October 24 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 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
CS 46B: Introduction to Data Structures June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design November 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Interfaces and Polymorphism CS 162 (Summer 2009).
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Interface types and Polymorphism
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Chapter 4 Interface Types and Polymorphism Part 1
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interfaces and Inheritance
CS 151: Object-Oriented Design October 8 Class Meeting
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 2 Assignment #2 Review  What are the primary classes? Game Controller Human Player Computer Player Typed in after a prompt Graphical user interface (press the rock, paper, or scissors button) Randomly Smart algorithm etc. Can vary. Must encapsulate! Ways to input the human player’s throw Ways to calculate the computer player’s throw

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 3 Assignment #2 Review  Encapsulate calculating the computer’s throw. ThrowCalculator calculateThrow(): Throw ComputerPlayer RandomThrow calculateThrow(): Throw SmartThrow calculateThrow(): Throw GeniusThrow calculateThrow(): Throw Abstract superclass Calendar Gregorian Calendar Lunar Calendar Recall:

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 4 Assignment #2 Review  Preserve your design flexibility! What’s wrong with this code? RandomThrow randomThrow = new RandomThrow(...); Throw t = randomThrow.calculateThrow(...); Use the Factory Method Design Pattern and code to the interface. ThrowCalculator calc = ThrowCalculator.makeCalculator(type,...); Throw t = calc.calculateThrow(...); ThrowCalculator calculateThrow(): Throw ComputerPlayer RandomThrow calculateThrow(): Throw In this example, coding to the interface means coding to the superclass ThrowCalculator.

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 5 Quiz #2 2013Sept26

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 6 Feed Our Pets  Suppose you have an array animals of animal objects: Animal MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter Animal animals[];

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 7 Feed Our Pets  Recall the definition of interface HouseholdPet :  How we find and feed all our pets that are in array animals ? public interface HouseholdPet { void feed(Food f); } for (int i = 0; i < animals.length; i++) { if ((animals[i] != null) && (animals[i] instanceof HouseholdPet)) { HouseholdPet pet = (HouseholdPet) animals[i]; Food f = Food.make(pet); // factory method pet.feed(f); } }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 8 Feed Our Pets  Instead of: you can write: HouseholdPet pet = (HouseholdPet) animals[i]; pet.feed(f); ((HouseholdPet) animals[i]).feed(f); The first way may be easier to write and read! _

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 9 Marker Interfaces  What if an interface type doesn’t declare anything? Suppose public interface Biter { // nothing! }  A marker interface is an empty interface that serves to “tag” the classes that implement it. Use the instanceof operator to see which objects are from a tagged class. _

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 10 Avoid Biters!  Example of how to use a marker interface: for (int i = 0; i < animals.length; i++) { if ((animals[i] != null) && (animals[i] instanceof Biter)) { // Code that does something // with animals that are biters. // // The code doesn’t call any // Biter methods (because // there aren’t any). } Animal animals[];

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 11 Polymorphism  The code of method showMessageDialog() needs the width of the icon that was passed in as a parameter. Interface Icon declares a getIconWidth() method. public static void showMessageDialog(Component parent, Object message, String title, int messageType, Icon anIcon) public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 12 Polymorphism  Any object passed as the anIcon parameter be must be instantiated from a class that implements the Icon interface. Method showMessageDialog() doesn’t care whether the parameter is an instance of ImageIcon or MarsIcon, or any other object, as long as the object was instantiated from a class that implements the Icon interface. Method showMessageDialog() calls method getIconWidth() on the parameter.  Implementations of method getIconWidth() may all be different with nothing in common other than the name and return type. public static void showMessageDialog(Component parent, Object message, String title, int messageType, Icon anIcon)

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 13 Polymorphism The feed() method of class Dog and the feed() method of class Goldfish may have very different implementations. public interface HouseholdPet { void feed(Food f); } public class Dog extends Mammal implements HouseholdPet { public void feed(Food f) { // code to feed a dog }... } public class Goldfish extends Fish implements HouseholdPet { public void feed(Food f) { // code to feed a goldfish }... }  Recall our interface HouseholdPet.

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 14 Polymorphism  Even though the feed() method of each HouseholdPet object may be different, we can call each feed() method the same way. Our code is flexible: It doesn’t matter which Animal it is, as long as it’s a HouseholdPet. for (int i = 0; i < animals.length; i++) { if ((animals[i] != null) && (animals[i] instanceof HouseholdPet)) { HouseholdPet pet = (HouseholdPet) animals[i]; Food f = Food.make(pet); // factory method pet.feed(f); } }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 15 Polymorphism  Polymorphism: The ability to appear in many forms.  In Java, polymorphism is The ability of a variable to automatically change behavior according to what object it refers to. The ability to automatically select the appropriate method M for a particular object, when  there are multiple objects of different classes that all implement an interface that declares method M.  there are multiple objects of different subclasses of a superclass that declares method M. _

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 16 Advantages of Polymorphism  Loose coupling Class JOptionPane uses the Icon interface but is decoupled from the MarsIcon or the ImageIcon classes.  Extensibility You can develop new types of icon objects, as long as their classes implement the Icon interface.

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 17 The Comparable Interface Type  The Java Collections class has a static sort() method that can sort any array list: The list can contain objects of an arbitrary class, such as Animal.  How can the sort() method sort the list? How can it determine whether one object in the list is greater than, less than, or equal to another object?  Answer: The class of the objects in the list must implement the Comparable interface. Collections.sort(list); ArrayList aList;

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 18 The Comparable Interface Type, cont’d  Suppose class Animal implements interface Comparable : public interface Comparable { int compareTo(T other); } public class Animal implements Comparable {... }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 19 The Comparable Interface Type, cont’d  Now class Animal must implement the interface’s compareTo() method.  The call object1.compareTo(object2) is expected to return: a negative value if object1 should come before object2 0 if both object1 and object2 are equal a positive value if object1 should come after object2 public interface Comparable { int compareTo(T other); }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 20 The Comparable Interface Type, cont’d  Let’s compare animals by their weights: public class Animal implements Comparable { int weight; int height; public int compareTo(Animal other) { if (weight < other.weight) return -1; if (weight > other.weight) return +1; return 0; }... } Of course, all the animal subclasses inherit the weight field and the compareTo() method.

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 21 The Comparator Interface Type  Suppose we might want to compare not just by the animal’s weights, but also by their heights, or perhaps by some other field. So the comparison method can vary! Therefore, we must... ??  A second form of method Collections.sort() takes two parameters: 1. the array list to sort 2. an object of a class that implements the Comparator interface public interface Comparator { int compare(T first, T second); }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 22 The Comparator Interface Type  To sort an Animal array list aList by height: public class AnimalComparatorByHeight implements Comparator { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } Comparator comp = new AnimalComparatorByHeight(); Collections.sort(aList, comp);

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 23 The Comparator Interface Type  Let the sort be ascending or descending: public class AnimalComparatorByHeight implements Comparator { private int dir; public AnimalComparatorByHeight(boolean ascending) { dir = ascending ? 1 : -1; } public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1*dir; if (animal1.height > animal2.height) return +1*dir; return 0; }

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 24 The Comparator Interface Type  Now to sort the Animal array list aList in descending order: Comparator comp = new AnimalComparatorByHeight(false); Collections.sort(aList, comp);  We can shorten the above code. Variable comp really isn’t necessary if we don’t use it again. Collections.sort(aList, new AnimalComparatorByHeight(false)); ArrayList aList;

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 25 Anonymous Classes public class AnimalComparatorByHeight implements Comparator { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } Comparator comp = new AnimalComparatorByHeight(); Collections.sort(aList, comp);

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 26 Anonymous Classes  Notice that in our program, after we declare class AnimalComparatorByHeight, we use the class in only one statement: Comparator comp = new AnimalComparatorByHeight();  We can shorten our code even further by not giving the class a name in a separate class definition. Define the body of the class only when it’s used. Make it an anonymous class. _

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 27 Anonymous Classes Comparator comp = new Comparator () { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } }; Collections.sort(aList, comp); Interface that the class implements Note the semicolon Comparator comp = new AnimalComparatorByHeight(); Collections.sort(aList, comp);

SJSU Dept. of Computer Science Fall 2013: September 26 CS 151: Object-Oriented Design © R. Mak 28 Anonymous Classes  We can even get rid of variable comp : Collections.sort(aList, new Comparator () { public int compare(Animal animal1, Animal animal2) { if (animal1.height < animal2.height) return -1; if (animal1.height > animal2.height) return +1; return 0; } }); Note the closing parenthesis Collections.sort(aList, comp);