Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of OO Introduction to Eclipse/Java September 6, 2005.

Similar presentations


Presentation on theme: "Review of OO Introduction to Eclipse/Java September 6, 2005."— Presentation transcript:

1 Review of OO Introduction to Eclipse/Java September 6, 2005

2 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation2 Object Oriented paradigm An approach to the solution of problems in which all computations are performed in the context of objects. The objects are instances of classes, which: —are data abstractions —contain procedural abstractions that operation on the objects A running program can be seen as a collection of objects collaborating to perform a given task

3 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation3 Procedural vs OO Design

4 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation4 2.2 Objects and Classes Object A chunk of data in a running software system —Member of a Java class Has properties that represent its state —Has Java instance variables Has behaviour that defines how it can act and react —Java methods Represented in UML by a named box with values assigned A class: Represents objects that share the same properties and behavior Represented in UML by a named box

5 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation5 Example Objects Margaret: date of birth: 1980/03/03 position: Teller Transaction 487: amount: 200.00 time: 2001/09/01 14:30 Greg: date of birth: 1970/01/01 address: 75 Object Dr. Mortgage Account 29865: balance: 198760.00 opened: 2000/08/12 property: 75 Object Dr. Instant Teller 876: location: Java Valley Cafe Savings Account 12876: balance: 1976.32 opened: 1997/03/03 Jane: date of birth: 1955/02/02 position: Manager address: 99 UML St. address: 150 C++ Rd.

6 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation6 Classes vs Instances (Objects) Something should be a class if it could have instances Something should be an instance if it is clearly a single member of the set defined by a class Film Class; instances are individual films. Reel of Film: Class; instances are physical reels Film reel with serial number SW19876 Instance of ReelOfFilm Science Fiction Instance of the class Genre. Science Fiction Film Class; instances include ‘Star Wars’ Showing of ‘Star Wars’ in the Phoenix Cinema at 7 p.m.: Instance of ShowingOfFilm

7 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation7 2.3 Instance Variables Class variables that hold data present in each instance Attributes —Simple data —E.g. name, dateOfBirth Associations —Relationships to other important classes —E.g. supervisor, coursesTaken —More on these in Chapter 5

8 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation8 Variables, Objects, and Methods A variable References an object May refer to different objects at different points in time Type of a variable Determines the class to which its referenced object belongs Method An abstraction that specifies a particular behaviour Independent of any code which implements that behaviour —E.g., calculating area (in general) Different classes can have methods with the same name

9 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation9 2.5 Organizing Classes: Inheritance Hierarchies Superclasses Contain features common to a set of subclasses. I.e., every subclass is a special kind of its superclass (isa rule) Inheritance hierarchies Show the relationships between superclasses and their subclasses —A UML triangle shows this relationship —Java uses extends to implement this relationship Inheritance The possession by a subclass of all the features (instance variables and methods) defined in its superclass

10 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation10 An Example Inheritance Hierarchy

11 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation11 The Isa Rule Always check subclasses to ensure they obey the isa rule “A checking account is an account” “A town is a municipality” “An undergraduate is a student” Should ‘State’ be a subclass of ‘Country’? No, it violates the isa rule: “A state is a country” doesn’t make sense. Rather, a country has provinces as its parts. —Thus, states are better modeled as a collection of instance variables for a country

12 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation12 A possible inheritance hierarchy of mathematical objects

13 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation13 Make Sure all Inherited Features Make Sense in Subclasses

14 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation14 2.6 Inheritance, Polymorphism and Variables

15 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation15 Some Operations in the Shape Example

16 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation16 Abstract Classes and Methods Each method should be declared in the class at the highest level in the hierarchy where it makes sense A method may be abstract (unimplementable) at that level —Java uses abstract to denote this —If so, the class also becomes abstract —No objects can be defined for an abstract class If a superclass has an abstract method then its subclasses must implement that method at some level —Leaf classes in the hierarchy must have no abstract methods

17 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation17 Overriding A non-abstract method can be redefined in a subclass where it would normally be inherited. For restriction —E.g. scale(x,y) would not work in Circle For extension —E.g. SavingsAccount might charge an extra fee following every debit For optimization —E.g. The getPerimeterLength method in Circle is much simpler than the one in Ellipse

18 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation18 An object is immutable if: Instance variables are initialized only when the object is first created. None of the methods can change the values of an instance variable —E.g. a scale method could only create a new object, not modify an existing one This is like a constant.

19 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation19 Dynamic binding Occurs when decision about which method to run can only be made at run time Needed when: —A variable is declared to have a superclass as its type, and —There is more than one possible (polymorphic) method that could be run for that type and its subclasses

20 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation20 Java Concepts: Documentation Looking up classes and methods is an essential skill Looking up unknown classes and methods will get you a long way towards understanding code Java documentation can be automatically generated by a program called Javadoc Documentation is generated from the code and its comments You should format your comments as shown in some of the book’s examples —These may include embedded html

21 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation21 Java Concepts: Arrays and Collections Arrays are of fixed size and lack methods to manipulate them Vector is the most widely used class to hold a collection of other objects More powerful than arrays, but less efficient Iterator s are used to access members of Vector s Enumeration s were formally used, but were more complex v = new Vector(); Iterator i = v.iterator(); while(i.hasNext()) { aMethod(v.next()); }

22 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation22 Java Concepts: Casting Java 1.4 is very strict about types If a variable is declared with type X, you can only invoke methods on it that are defined in class X or its superclasses. If you know that a particular subclass is stored, then you can cast the variable to the subclass —E.g. if I know a Vector contains instances of String, I can get the next element of its Iterator using: (String)iterator.next(); Java 1.5 is more flexible in this regard (casting is often automatic)

23 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation23 Java Concepts: Exceptions Anything that can go wrong should result in the raising of an Exception Exception is a class with many subclasses for specific things that can go wrong Use a try - catch block to trap an exception try { // some code } catch (ArithmeticException e) { // code to handle division by zero }

24 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation24 Java Concepts: Interfaces Like abstract classes, but cannot have executable statements Define a set of methods that are useful in several classes A class can implement any number of interfaces It must implement all of the interface’s methods You can declare the type of a variable to be an interface This is just like declaring the type to be an abstract class Important interfaces in Java’s library include Runnable, Collection, Iterator, Comparable, Cloneable

25 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation25 Java Concepts: Packages and Importing A package combines related classes into a subsystem All the classes are placed in the same directory, which names the package Each such class may have a header line identifying the package to which it belongs: package kk2Client; Importing all the classes in a package is specified in the header line of a class file: import finance.banking.accounts.*;

26 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation26 Java Concepts: Access control Applies to methods and variables public —Any class can access protected —Only code in the package, or subclasses can access (blank) —Only code in the package can access private —Only code written in the class can access —Inheritance still occurs!

27 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation27 Java Concepts: Threads and concurrency Thread: A sequence of executing statements that can be running concurrently with other threads To create a thread in Java: 1.Create a class implementing Runnable or extending Thread 2.Implement the run method as a loop that does something for a period of time 3.Create an instance of this class 4.Invoke the start operation, which calls run

28 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation28 Java Concepts: Style Guidelines Write readable code Always choose the simpler alternative Reject clever code that is hard to understand Shorter code is not necessarily better Avoid code duplication (make a new method and call it) Choose descriptive names (don’t worry about length) Naming classes Use capital letters and singular nouns, E.g. BankAccount Use the right level of generality, E.g. Municipality, not City Make sure the name has one meaning, E.g. ‘ bus ’ a poor choice Prefer private over public (information hiding) Do not mix user interface code with non-user interface code Interact with the user in separate classes

29 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation29 Overview of Eclipse A general software design environment that: —Supports Java —Supports UML modeling —Supports Java documentation —Is highly flexible and adaptable to different languages —Has extensive on-line tutorial and help information Eclipse runs on OS X and Windows machines —Freely downloadable Our lab version is Eclipse 3.0.1 (www.eclipse.org) with: —Java 1.4 (java.sun.com) —Omondo UML (www.omondo.com) —JML (www.cs.iastate.edu/~leavens/JML/index.shtml)

30 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation30 Eclipse: Startup

31 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation31 Eclipse: Opening the workbench

32 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation32 Eclipse: Opening the Java Perspective

33 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation33 Eclipse: creating a new Java project

34 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation34 Eclipse: naming the project

35 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation35 Eclipse: Importing Java code into a project

36 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation36 Eclipse: Creating a run configuratioin

37 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation37 Eclipse: Running a program (thread)

38 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation38 Eclipse: Running another program (thread)

39 © Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation39 Eclipse: Switching between threads


Download ppt "Review of OO Introduction to Eclipse/Java September 6, 2005."

Similar presentations


Ads by Google