35
Object-Oriented Design Object-Oriented Design CSC 212

Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Embed Size (px)

Citation preview

Page 1: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Object-Oriented DesignObject-Oriented Design

CSC 212

Page 2: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Announcements

This course is speeding up and we are starting new material. Please see me if you feel this is going too fast for you.CSC tutoring hours posted outside Wehle 208

I’m new to Canisius. Feel free to give me feedback about how this class is going. I will always listen and appreciate your adviceCannot guarantee I will follow suggestions

Page 3: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Announcements

If you need additional Java review, seek out help nowHomework #1 will on web tonightWill be due next Thursday (9/22)

Students should use Wehle 208 for their homework If you need, ask me for the access passwords

Page 4: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Questions?

Page 5: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Object-Oriented Design

Goodrich & Tamassia (1st edition), p. 5

Page 6: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Adaptability

How easy is it for the code to evolve?What assumptions are in the design?Are the assumptions stated explicitly?What will happen when the assumptions

become incorrect?How easy is porting the code to a different

environment (portability)?

Page 7: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Robustness

Will the program perform correctly?What assumptions are made about the input?What happens if incorrect input is entered?Can the design expand to handle larger

datasets?Can the program fail gracefully?

Page 8: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Reusability

Can the code be incorporated into other projects?Does it clearly describe what it can do?Does it clearly describe what it can’t do? Is it difficult to understand how to use it? Is it easy to add extra functionality?

Page 9: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Achieving Design Goals

Three principles develop code fulfilling design goalsAbstractionEncapsulationModularity

Page 10: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Abstraction

Distill system to most fundamental parts Important actors should be made into classesEach class describes a single actor/data type

From lab: Rectangle & Vertex should be classes

Designs become adaptable Classes describe important actors more generally Resulting code not specific to problem

Page 11: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Abstraction

Describe design in simple, easy languagePublic methods define actions important to

user Method name active verb describing functionality Enhances reuse – code functionality is obvious

Limiting demands on each method improves robustness

Simplifies testing, also!

Page 12: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Encapsulation

Hide details not needed by userMake implementations methods private

Provides freedom in how code is implemented Improves reuse – easy to understand how to use

class Improves adaptability –change implementation

without needing to change how code is used

Page 13: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Modularity

Organize functional units into separate modulesOrganize separate ideas into separate places

Classes represent only one type of actor Methods perform only one action

Use hierarchical nature of Java to enable sharing of similar code

Page 14: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Hierarchical Nature of Data

Multiple ideas may share actions or dataWant to implement ideas as separate classesBut only one place should contain methods &

fields Java enables this through inheritence

Generic superclass contains shared code & data

Other extend or specialize the superclass

Page 15: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Hierarchical Nature of Data

Example: the Rectangle classWant new class of Rectangle OutlinesBut, cannot change Rectangle class

People are already using previous code May not have access to source code Already tested and documented

Page 16: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Hierarchical Nature of Data

Create RectangleOutline extending Rectangle Add new field (outline) method isOutline()Modify method returning if point is within the

Rectangle A point is within RectangleOutline only if it is on

one of the edges

Rectangle

RectangleOutline

parent classsuper class

child classsubclass

shows “is a” relationship

Page 17: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Data Hierarchies

This relationship could be continued Consider the different rectangles drawn

used for windowsRectangles with Title BarsButtons

Buttons with text Buttons with pictures Buttons with text & pictures

Page 18: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Class Inheritance Diagram

Goodrich & Tamassia (2nd edition), p. 62

Page 19: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Where Does This Go?

Cannot treat classes interchangeablyE.g. Getting picture from TextButton

But sometimes we canAll Rectangles have location and can ask if a

point is inside boundaries Deciding where methods belong is big part

of creating class hierarchy

Page 20: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Class Hierarchy

ButtonRectWithTitle

Rectangleboolean isInternal(Point p)

void move(float offX, float OffY)

String getTitleBarText()

void minimizeWindow()

boolean buttonPushed()

Color getColor()

Page 21: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Inheritance

Classes inherit all fields from its superclass, the superclass’ superclass…

Similarly, classes inherit methods down through the class hierarchy

Organize fields and methods toLimit duplication Make fields & methods useful to ALL subclasses

which inherit them

Page 22: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

TextButton

/** * This can represent a button containing text */public class RectWithTitle extends Rectangle { String titleBarText; // If currently an outline

/** Constructor invokes Rectangle() constructor */ public RectangleOutline(Point lowL, Point topR,

String titleBarText) { super(lowL, topR);

this.titleBarText = titleBarText; } …}

1

2

3

Page 23: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Keyword Meanings

1. extends links two classes – the declared class becomes subclass of the other one

2. super enables accessing methods in the superclass; in this case, a Rectangle constructor

3. this specifies access is to member of the current instance.

Page 24: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

What Gets Inherited And How?

All public members inherited, stay publicPackage members similarly passed down

Subclass can access protected members as if they were publicEven for instances of the superclass type!

Private members remain in superclass

Page 25: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Polymorphism

Comes from Greek meaning “many forms” Instances of Button can be used like a

Rectangle, since a Button is a RectangleThis is an example of polymorphism

Cannot use Rectangle as Button, howeverCannot use RectangleOutline as Button either

Page 26: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Java’s Object Hierarchy

Class can extend at most one other class If no superclass given, implicitly extend ObjectSo all classes are subclasses of Object

Object defines useful methods, including:public boolean equals(Object obj)protected Object clone() throws

CloneNotSupportedExceptionpublic String toString()

Page 27: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Reusing Names in Classes

Can reuse method names in subclasses Overload method declaration

Subclass defines method with same name, different signatures

Override method declarationSubclass defines method with same name &

signatureSubclass method replaces inherited one

Page 28: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Overloading Methods

Same method name can be used with different data typesConsider how the operator “+” works on int,

float, and Strings Overloading allow subclasses to expand

how an action is defined in superclassBut keeps original methods available, too

Page 29: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Overloading Methods

Overloaded methods differ in argument listsWorks like multiple constructor definitions

Which method executed depends on call parametersE.g., Calls the method which matches the

number, type, and order of argumentsCan methods differ only in return type?

Page 30: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Overriding Methods

Subclass provides new implementation for inherited methodUses same name & signatureWhat return types could be used?

Subclass follows superclass’ throws clause Inherited method cannot use more restrictive

access control

Page 31: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Overriding Methods

When calling method, actual class of instance determines which implementation is executedE.g., Rectangle p1 = new RectangleOutline(…); p1.isInternal(point); // What is called?

Page 32: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Overriding Example

public class SuperClass {

public String getMyString() {return “SUPERSTRING”;}

public SuperClass() { }

}

public class SubClass extends SuperClass {

public String getMyString() {return “substring”;}

public SubClass() { }

public static void main(String[] args) {

SubClass sub = new SubClass();

SuperClass super = sub;

String test1 = sub.getMyString(); // test1 ==

String test2 = super.getMyString(); // test2 ==

super = new SuperClass();

test2 = super.getMyString(); // test2 =

...

Page 33: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Reusing Fields in Subclasses

Subclasses can hide superclass’s fieldsSubclass defines field with identical nameField from superclass also exists in subclass

super.<fieldname> accesses superclass’s field

Which field Java will access? What does this depend on?

Page 34: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Field Hiding Example

public class SuperClass { public SuperClass() { } protected String myString = “SUPERSTRING”; public String getMyString() { return myString; }}

public class SubClass extends SuperClass { public SubClass() { } protected String myString = “substring”; public String getMyString() { return myString; }

Page 35: Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too

Field Hiding Example

public static void main(String[] args) {

SubClass sub = new SubClass();

SuperClass super = subInstance;

String test1, test2;

test1 = sub.getMyString(); // test1 ==

test2 = super.getMyString(); // test2 ==

test1 = sub.myString; // test1 ==

test2 = super.myString; // test2 ==

}

}