Object-Oriented Programming: Classes, Inheritance, and Interfaces

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Inheritance using Java
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Object-Oriented Concepts
Web Design & Development Lecture 9
OOP: Encapsulation &Abstraction
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
Objects as a programming concept
Software Development Java Classes and Methods
Programming Language Concepts (CIS 635)
Object Oriented Programming
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Overloading and Constructors
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
The Building Blocks Classes: Java class library, over 1,800 classes:
Interfaces.
OO Design with Inheritance
Java Inheritance.
Java Programming Language
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CIS 110: Introduction to computer programming
Presentation transcript:

Object-Oriented Programming: Classes, Inheritance, and Interfaces Wellesley College CS230 Lecture 06 Thursday, February 15 Handout #14 Reading: Downey: Chapter 13; Carrano: p. 126-134 Problem Set: Assignment #2 due Friday, Feburary 23

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.

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.

Class Example: A Counter Class 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(); 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();}

Information Flow: Public/Protected/Private Within a class C, any phrase (statement/expression) P can access: public class variables of any class; protected class variables of classes in the same package as C; private class variables of C; public instance variables of any object mentioned in P; protected instance variables of any object O mentioned in P, where O’s class is in the same class as C. private instance variables of any instance of C. Additionally: Any method body can access all parameter variables. A constructor method body can access the newly constructed instance via this. An instance method body can access the receiver object via this.

When to use private and protected? Private should be the default: This helps to preserve invariants: e.g., counter id is never changed (immutable); counters in the all vector are sorted by id. Gives flexibility to implementer to change representations under the hood. Protected is used for instance/class variables that need to be accessed by other classes in the same package, particularly subclasses of the given class. E.g., we’ll soon see subclasses of Counter that need to access/change the count instance variable. Public should only be used for instance/class variables as public when (1) their implementation will never change and (2) it’s OK for the whole world to see/change their value. (Can prevent changing public variables like Color.red by declaring them final.)

Inheritance: A Resettable Counter public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer Object subclasses public Counter (); public void inc (); // increments count public String toString(); // Overrides Object’s definition Counter superclasses Resettable Counter public ResettableCounter(); public void reset (); // resets count to 0

Inheritance: Resettable Counter Code public class ResettableCounter extends Counter { public void reset() { count = 0; // Won’t work if count is declared private in Counter! displayCounters(); } public static void main (String [] args) { ResettableCounter a = new ResettableCounter(); // [1 of 1:0]; a.inc(); // [1 of 1:1]; Counter b = new Counter(); // [1 of 2:1]; [2 of 2:0]; a.inc(); // [1 of 2:2]; [2 of 2:0]; b.inc(); // [1 of 2:2]; [2 of 2:1]; a.reset(); // [1 of 2:0]; [2 of 2:1]; a.inc(); // [1 of 2:1]; [2 of 2:1]; // The following is implicitly declared: public ResettableCounter() { super(); }

Inheritance: An Inittable Counter public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer Object subclasses public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition Counter superclasses Resettable Counter public ResettableCounter(); public void reset (); // resets count to 0 Inittable Counter public InittableCounter(); public InittableCounter(int init); // initializes count to init

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]; }

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;

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 subclasses Counter superclasses Resettable 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 Inittable Counter

Inheritance: An Equatable Counter public Object(); public boolean equals (Object x); // this = x public String toString(); // string rep. of pointer Object subclasses public Counter (); public void inc (); // increments count public String toString(); // overrides Object’s definition Counter superclasses Resettable Counter public ResettableCounter(); public void reset (); // resets count to 0 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

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; } 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 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(r) → false

Two Implementations of Immutable Points public class IPointVars { private int x, y; // Represent coords with two int variables. public IPointVars (int x, int y) {this.x = x; this.y = y;} public int getX() { return x; } public int getY() { return y; } public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointArray { private int[] ints = new int[2]; // Represent coords with two-slot array. public IPointArray (int x, int y) {ints[0] = x; ints[1] = y;} public int getX() { return ints[0]; } public int getY() { return ints[1]; } public String toString() { return "(" + getX() + "," + getY() + ")"; } }

Manipulating Immutable Points public static int sumCoordsVars (IPointVars p) { return p.getX() + p.getY(); } public static int sumCoordsArray (IPointArray p) { The sumCoord methods are the same modulo the point type. Can we define a single sumCoords() method that works on instances of both classes? Yes! By using (1) an interface or (2) an abstract class.

An IPoint Interface public interface IPoint { public int getX(); public int getY(); public String toString(); } public class IPointVars implements IPoint { … same impl. as before …} public class IPointArray implements IPoint { … same impl. as before …} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) → 3 sumCoords(new IPointArray(3,4)) → 7 An interface specifies the types of some instance method contracts. Gives new information to Java type checker

An IPoint Abstract Class A class must be declared abstract if any of its methods are declared abstract. public abstract class IPoint { public abstract int getX(); public abstract int getY(); public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointVars extends IPoint { … same impl. as before except no toString() method …} public class IPointArray extends IPoint { … same impl. as before except no toString () method …} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) → 3 sumCoords(new IPointArray(3,4)) → 7 A method without a body is declared abstract. An abstract class may have concrete methods; these can use abstract methods. Use extends rather than implements with abstract class

Enumerations: A Very Handy Interface

Motivating Enumerations: Manipulating File Lines

A File Line Enumeration

Using the File Line Enumeration

Comparable: Another Handy Interface