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

Slides:



Advertisements
Similar presentations
Solutions to Review Questions. 4.1 Define object, class and instance. The UML Glossary gives these definitions: Object: an instance of a class. Class:
Advertisements

ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Introduction To System Analysis and Design
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 2: Review of Object Orientation.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
Chapter 10 Classes Continued
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 2: Review of Object Orientation 1.
Introduction To System Analysis and design
© Lethbridge/Laganière 2005 Chapter 2: Review of Object Orientation1 Warm Up Question: Draw a Pert diagram for the following tasks: TaskPreceding TaskDescriptionDuration.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Programming Languages and Paradigms Object-Oriented Programming.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Basic Concepts Introduction to OO Development and Software Engineering Principles SYSC System Analysis and Design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 2: Review of Object Orientation.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Review of object Orientation Lecturer: Dr. Mai Fadel.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Salman Marvasti Sharif University of Technology Winter 2015.
Object-Oriented Programming Chapter Chapter
M1G Introduction to Programming 2 5. Completing the program.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CSE 240 Lecture 7. © Lethbridge/Laganière 2001 Chapter 5: Modelling with classes2 Overview Hopefully return and discuss test 1 A brief discussion of Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Principles of Programming. Achieving an Object-Oriented Design  Abstraction and Information Hiding  Object-Oriented Design  Functional Decomposition.
Polymorphism and Inheritance (CS-319 Supplementary Notes)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCE 240 – Intro to Software Engineering Lecture 3.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 2: Review of Object Orientation.
Chapter 11 Object-Oriented Design
Lecture 22 Inheritance Richard Gesick.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 2: Review of Object Orientation
Presentation transcript:

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

© 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

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

© 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

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

© 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

© 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

© 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

© 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

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

© 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

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

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

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

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

© 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

© 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

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

© 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

© 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

© 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()); }

© 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)

© 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 }

© 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

© 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.*;

© 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!

© 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

© 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

© 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 ( with: —Java 1.4 (java.sun.com) —Omondo UML ( —JML (

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

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

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

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

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

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

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

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

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

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