Download Object-Oriented Programming: Classes, Inheritance, and Interfaces and more Assignments Data Structures and Algorithms in PDF only on Docsity! 1 06- 1 Object-Oriented Programming: Classes, Inheritance, and Interfaces Reading: Downey: Chapter 13; Carrano: p. 126-134 Problem Set: Assignment #2 due Friday, Feburary 23 Wellesley College CS230 Lecture 06 Thursday, February 15 Handout #14 06- 2 Object-Oriented Programming (OOP) • In OOP, computations are described in terms of collections of stateful objects that communicate by passing messages to one other. • Objects are like actors in a play; the programmer is the playwright & director. • A class specifies the behavior (variables and methods) of a collection of objects = the instances of the class. • Related classes can be organized into inheritance hierarchies, which allow one class to extend and/or override the variables and methods of other classes. • OOP is one of several programming paradigms. Others include imperative programming, function-oriented programming, logic programming. CS251 Programming Languages covers these. • IMHO, OOP is overhyped – function-oriented programming with state (as embodied in Scheme & OCAML) does everything OOP can do plus much, much more! Take CS251 to See The Light. 2 06- 3 The Anatomy of a Java Class A Java class can contain 5 kinds of declarations: • 2 kinds of variable declarations: – an instance variable for every instance of the class – a class (static) variable in exactly one place (the class itself) • 3 kinds of method declarations. – A constructor method specifies how to create a new instance. Via this, it can refer to all (including private) instance variables of the new instance that are declared in the method’s class. – An instance method specifies how an instance (the receiver = this) responds to a message. It can also refer to all of the receiver’s instance variables that are declared in the method’s class. Note that the receiver is really just an argument with a special name (this). – A class method is a receiverless message that corresponds to functions/procedures in other languages. It cannot refer to this, since there is no receiver. 06- 4 Class Example: A Counter Class import java.util.*; // imports Vector public class Counter { // Instance variables: protected int count; private int id; // Class variable: private static Vector<Counter> all = new Vector<Counter>(); // Constructor method: public Counter () { all.add(this); this.id = all.size() // Here and below, can punt “this.” this.count = 0; displayCounters(); } // Instance methods: public void inc () { this.count++; displayCounters();} public String toString () { return "[" + this.id + “ of “ + all.size() + ":" + this.count + "]"; } // Class methods: public static void displayCounters() { for (int i = 0; i < all.size(); i++) { System.out.print(all.get(i) + “; “); } System.out.println(); } public static void main (String [] args) { Counter a = new Counter(); a.inc(); Counter b = new Counter(); a.inc(); b.inc(); a.inc(); } } 5 06- 9 Inheritance: An Inittable Counter Object Counter Resettable Counter public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition public ResettableCounter(); public void reset (); // resets count to 0 su pe rc la ss es subclasses Inittable Counter public InittableCounter(); public InittableCounter(int init); // initializes count to init 06- 10 Inheritance: Inittable Counter Code public class InittableCounter extends ResettableCounter { public InittableCounter (int init) { super(); // Call nullary Counter constructor; count = init; displayCounters(); } public InittableCounter () { this(0); } public static void main (String [] args) { InittableCounter a = new InittableCounter(17); // [1 of 1:0]; // [1 of 1:17]; a.inc(); // [1 of 1:18]; InittableCounter b = new InittableCounter(); // [1 of 2:18]; [2 of 2:0]; // [1 of 2:18]; [2 of 2:0]; a.inc(); // [1 of 2:19]; [2 of 2:0]; b.inc(); // [1 of 2:19]; [2 of 2:1]; a.reset(); // [1 of 2:0]; [2 of 2:1]; a.inc(); // [1 of 2:1]; [2 of 2:1]; } } 6 06- 11 Common Constructor Method Gotchas public InittableCounter (int count) { count = count; // X // use this.count = count; } public InittableCounter (int init) { int count = init; // X // use count = init; } 06- 12 The instanceof Operator Counter c = new Counter(); c instanceof Object → true c instanceof Counter → true c instanceof ResettableCounter → false c instanceof InittableCounter → false c instanceof Point → false Object Counter Resettable Counter su pe rc la ss es subclasses Inittable Counter InittableCounter i = new InittableCounter(); i instanceof Object → true i instanceof Counter → true i instanceof ResettableCounter → true i instanceof InittableCounter → true i instanceof Point → false 7 06- 13 Inheritance: An Equatable Counter Object Counter Resettable Counter public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition public ResettableCounter(); public void reset (); // resets count to 0 su pe rc la ss es subclasses Inittable Counter public InittableCounter(); public InittableCounter(int init); // initializes count to init Equatable Counter public EquatableCounter(); public boolean equals (Object x); // this.count = x.count 06- 14 Inheritance: EquatableCounter Code public class EquatableCounter extends InittableCounter { public boolean equals (Object x) { if (! (x instanceof Counter)) { return false; } else { Counter c = (Counter) x; // Downcast will succeed return this.count == c.count; } } e.equals(c) → true // asymmetric! e.equals(c2) → true e.equals(c3) → true e.equals(r) → true c.inc(); e.inc(); e.equals(c) → true e.equals(c2) → false e.equals(c3) → true e.equals(r) → false Counter c = new Counter(); Counter c2 = new Counter(); Counter c3 = c; Counter r = new ResettableCounter(); Counter e = new EquatableCounter(); c.equals(c2) → false c.equals(c3) → true c.equals(r) → false c.equals(e) → false