Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Object-Oriented Design CSC 212

2 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 advice  Cannot guarantee I will follow suggestions

3 Announcements If you need additional Java review, seek out help now  Homework #1 will on web tonight  Will be due next Thursday (9/22) Students should use Wehle 208 for their homework  If you need, ask me for the access passwords

4 Questions?

5 Object-Oriented Design Goodrich & Tamassia (1st edition), p. 5

6 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)?

7 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?

8 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?

9 Achieving Design Goals Three principles develop code fulfilling design goals  Abstraction  Encapsulation  Modularity

10 Abstraction Distill system to most fundamental parts  Important actors should be made into classes  Each 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

11 Abstraction Describe design in simple, easy language  Public 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!

12 Encapsulation Hide details not needed by user  Make 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

13 Modularity Organize functional units into separate modules  Organize 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

14 Hierarchical Nature of Data Multiple ideas may share actions or data  Want to implement ideas as separate classes  But only one place should contain methods & fields Java enables this through inheritence  Generic superclass contains shared code & data  Other extend or specialize the superclass

15 Hierarchical Nature of Data Example: the Rectangle class  Want new class of Rectangle Outlines  But, cannot change Rectangle class People are already using previous code May not have access to source code Already tested and documented

16 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 class super class child class subclass shows “is a” relationship

17 Data Hierarchies This relationship could be continued Consider the different rectangles drawn used for windows  Rectangles with Title Bars  Buttons Buttons with text Buttons with pictures Buttons with text & pictures

18 Class Inheritance Diagram Goodrich & Tamassia (2 nd edition), p. 62

19 Where Does This Go? Cannot treat classes interchangeably  E.g. Getting picture from TextButton But sometimes we can  All Rectangles have location and can ask if a point is inside boundaries Deciding where methods belong is big part of creating class hierarchy

20 Class Hierarchy ButtonRectWithTitle Rectangle boolean isInternal(Point p) void move(float offX, float OffY) String getTitleBarText() void minimizeWindow() boolean buttonPushed() Color getColor()

21 Inheritance Classes inherit all fields from its superclass, the superclass’ superclass… Similarly, classes inherit methods down through the class hierarchy Organize fields and methods to  Limit duplication  Make fields & methods useful to ALL subclasses which inherit them

22 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

23 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.

24 What Gets Inherited And How? All public members inherited, stay public  Package members similarly passed down Subclass can access protected members as if they were public  Even for instances of the superclass type! Private members remain in superclass

25 Polymorphism Comes from Greek meaning “many forms” Instances of Button can be used like a Rectangle, since a Button is a Rectangle  This is an example of polymorphism Cannot use Rectangle as Button, however  Cannot use RectangleOutline as Button either

26 Java’s Object Hierarchy Class can extend at most one other class  If no superclass given, implicitly extend Object  So all classes are subclasses of Object Object defines useful methods, including:  public boolean equals(Object obj)  protected Object clone() throws CloneNotSupportedException  public String toString()

27 Reusing Names in Classes Can reuse method names in subclasses Overload method declaration  Subclass defines method with same name, different signatures Override method declaration  Subclass defines method with same name & signature  Subclass method replaces inherited one

28 Overloading Methods Same method name can be used with different data types  Consider how the operator “+” works on int, float, and Strings Overloading allow subclasses to expand how an action is defined in superclass  But keeps original methods available, too

29 Overloading Methods Overloaded methods differ in argument lists  Works like multiple constructor definitions Which method executed depends on call parameters  E.g., Calls the method which matches the number, type, and order of arguments  Can methods differ only in return type?

30 Overriding Methods Subclass provides new implementation for inherited method  Uses same name & signature  What return types could be used?  Subclass follows superclass’ throws clause  Inherited method cannot use more restrictive access control

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

32 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 =...

33 Reusing Fields in Subclasses Subclasses can hide superclass’s fields  Subclass defines field with identical name  Field from superclass also exists in subclass super. accesses superclass’s field Which field Java will access? What does this depend on?

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

35 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 == }


Download ppt "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."

Similar presentations


Ads by Google