PowerPoint Slides Object-Oriented Design Using JAVA by Dale Skrien Chapter 2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object-oriented Programming • Divides the program into a set of communicating objects • Encapsulates in an object all the behavior and knowledge related to one concept • Distributes control and data since the intelligence and knowledge is distributed among the objects Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object-oriented Languages • Support OO style of programming • Provide – classes – objects – messages – inheritance – polymorphism Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Java class public class Person {! private String name;! private Date birthdate;! public Person(String name, Date birthdate) {! this.name = name;! this.birthdate = birthdate;! }! public String getName() {! return name;! }! public Date getBirthdate() {! return birthdate;! }! }! Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.1. Java primitive & reference types. The variable x stores the primitive integer value 3 whereas the variable s stores a reference to the String object “hello”. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.2. The UML class diagram for the Person class. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.3. The UML notation for subclass and superclass. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Implementation Inheritance • Also called “subclassing” • Indicated in Java by keyword “extends” • Uses: – Specialization by adding new behavior in a subclass – Specialization by overriding existing behavior in a subclass – Generalization by creating a superclass and moving code up Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.4. Specialization by adding new behavior to the subclass. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance Example public class EnhancedRectangle extends Rectangle { public EnhancedRectangle(int x, int y, int w, int h) //constructor { super(x, y, w, h); } public Point getCenter() { return new Point((int) getCenterX(), (int) getCenterY()); } public void setCenter(int x, int y) { setLocation( x – (int) getWidth()/2, y – (int) getHeight()/2); } } Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. FilledOval subclass of Oval • Example of specialization by overriding existing methods • FilledOval class overrides the draw method of the Oval class to draw a filled-in black oval instead of a white oval with black border Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.5. Two white ovals with black borders and a filled oval. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Public class Oval { public int x, y, w, h; public Oval(int x, int y, int w, int h) { //constructor this.x = x; this.y = y; this.w = w; this.h = h; } public void draw(Graphics g) { g.drawOval(x, y, w, h); } public int getWidth() {return w;} public int getHeight() {return h;} public Point getTopLeftPoint() {return new Point(x,y);} // … other methods … } Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Specialization Example Public class FilledOval extends Oval { public FilledOval(int x, int y, int w, int h) { super(x, y, w, h); } public void draw(Graphics g) { g.fillOval(x, y, w, h); } } Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.6. Generalization by moving common behavior to a superclass. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Generalization Example public abstract class Customer {…} HumanCustomer human = new HumanCustomer(…); BusinessCustomer business = new BusinessCustomer(…); String humanName = human.getName(); String businessName = business.getName(); Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.7. A Person class with two superclasses. This is not legal in Java. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.8. Class C inherits two foo() methods. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Types and interfaces • A type is a set of data values and the operations that can be performed on them. • Example: The Java integer type has 232 values and has operations such as addition, subtraction, multiplication, and division. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Java classes as types • Each Java class is a type • The values of such a type are the objects of that class or any subclasses • The operations are the methods of the class Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Java interfaces as types • Each Java interface is a type • The values of such a type are the objects of all classes that implement the interface • The operations are the methods declared in the interface Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Subtypes • A type S is a subtype of type T if the set of objects of S form a subset of the set of objects of T and the set of operations of S are a superset of the set of operations of T • Example: Any subclass defines a subtype of its superclass Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.9. A SimpleRunner class that implements the Runnable interface and extends the Object class. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Subtype polymorphism • You can use an object of a subtype wherever an object of a supertype is expected. • Examples: List l = new ArrayList();! List l = new LinkedList(); Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.10. The List interface and two classes that implement it. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract classes • Abstract classes are denoted by the key word “abstract”. • Objects of such classes cannot be created. • Some of the methods can be declared abstract, in which case subclasses must implement those methods (or be abstract themselves). Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract classes vs. interfaces • Abstract classes can include method implementations and non-final fields • Abstract classes with no implementations are like interfaces except a class can extend only one superclass in Java. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic method invocation • In a method call v.foo(), the Java runtime environment looks at the actual class of the value of v, not the declared type of v, to determine which implementation of foo to execute. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic method invocation example • Suppose a class A has a method foo that prints “A” and a subclass B has a method foo that prints “B”. A v = new B(); !v.foo(); • The implementation of foo in class B is the one that is executed (“B” is printed). Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.11. The abstract Automobile class and its subclasses. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic method invocation for Automobiles Automobile[] fleet = new Automobile[]{! new Sedan(Color.black), ! new Minivan(Color.blue), ! new SportsCar(Color.red)};! int totalCapacity = 0;! for(Automobile car : fleet) {! totalCapacity += car.getCapacity();! }! Three different getCapacity methods are executed in this loop (one in each subclass) Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Overloading vs. Overriding • Overloading refers to two methods in the same class with the same name, but different parameter lists. • Overriding concerns two methods, one in a subclass and one in a superclass, with identical signatures (name and parameter list). Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.12. The Automobile class has two equals methods, one inherited and one defined in Automobile. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Figure 2.13. The Automobile class does not inherit the Object class’ equals method. Instead, it overrides that method. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Overloaded method example Object o = new Object();! Automobile auto = new Automobile();! Object autoObject = new Automobile();! auto.equals(o);! auto.equals(auto);! auto.equals(autoObject);! Which of the two equals methods in the Automobile class is executed in each of the last 3 lines above?! Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.