Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 Chapter 1 Object-Oriented.

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Chapter 10: Introduction to Inheritance
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
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
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
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.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
 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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Coming up: Inheritance
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
Chapter 15 Abstract Classes and Interfaces
OOP: Encapsulation &Abstraction
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 11 Object-Oriented Design
Chapter 10 Object-Oriented Modeling
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Thinking in Objects
Chapter 12 Abstract Classes and Interfaces
Chapter 9 Thinking in Objects
Java Programming, Second Edition
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 10 Thinking in Objects
Presentation transcript:

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented Design (..contd)

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Objectives F To design systems by identifying the classes and discovering the relationships among these classes (§12.4). F To implement the Rational class and process rational numbers using this class (§12.5). F To design classes that follow the class-design guidelines (§12.6). F To model dynamic behavior using sequence diagrams and statechart diagrams (§12.7 Optional) F To know the concept of framework-based programming using Java API (§12.8).

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Class Design 1. Identify classes for the system. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 4. Create classes.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Class Design Guidelines/Issues F Cohesion and Consistency: Designing a Single Class. F Encapsulation: Using Modifiers public, protected, private and static F Using Inheritance or Aggregation F Using Interfaces or Abstract Classes

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Designing a Class F A class should describe a single entity or a set of similar operations. F A single entity with too many responsibilities can be broken into several classes to separate responsibilities. F The String class, StringBuffer class, and StringTokenizer class all deal with strings, for example, but have different responsibilities.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Designing a Class, cont. F Classes are usually designed for use by many different customers. F To make a class useful in a wide range of applications, the class should provide a variety of ways for customization through properties and methods.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Designing a Class, cont. F Classes are designed for reuse. F Users can incorporate classes in many different combinations, orders, and environments. F Therefore, you should design a class that imposes no restrictions on what or when the user can do with it, design the properties to ensure that the user can set properties in any order, with any combination of values, and design methods to function independently of their order of occurrence.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Designing a Class, cont. F Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible. F Follow standard Java programming style and naming conventions. F Choose informative names for classes, data fields, and methods. F Always place the data declaration before the constructor, and place constructors before methods. F Always provide a constructor and initialize variables to avoid programming errors.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Visibility Modifiers F Each class can present two contracts – one for the users of the class and one for the extenders of the class. F Make the fields private and accessor methods public if they are intended for the users of the class. F Make the fields or method protected if they are intended for extenders of the class. F The contract for the extenders encompasses the contract for the users. F The extended class may increase the visibility of an instance method from protected to public, or change its implementation, but you should never change the implementation in a way that violates that contract.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Visibility Modifiers, cont. F A class should use the private modifier to hide its data from direct access by clients. F You can use get methods and set methods to provide users with access to the private data, but only to private data you want the user to see or to modify. F A class should also hide methods not intended for client use. F The gcd method in the Rational class in Example 12.2, “The Rational Class,” is private, for example, because it is only for internal use within the class.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using the static Modifier F A property that is shared by all the instances of the class should be declared as a static property.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation F The difference between inheritance (is-an- extension-of) and aggregation (has-a) F For example, an apple is fruit; thus, you would use inheritance to model the relationship between the classes Apple and Fruit. F A person has a name; thus, you would use aggregation to model the relationship between the classes Person and Name.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation, cont. F Sometimes, the choice between inheritance and aggregation is not obvious. F For example, you have used inheritance to model the relationship between the classes Circle and Cylinder. F One could argue that a cylinder consists of circles; thus, you might use aggregation to define the Cylinder class as follows:

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Inheritance or Composition, cont. public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ }

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Inheritance or Aggregation, cont. F Both designs are fine. Which one is preferred? F If polymorphism is desirable, you need to use the inheritance design. F If you don’t care about polymorphism, the aggregation design gives more flexibility because the classes are less dependent using aggregation than using inheritance.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Interfaces or Abstract Classes F Both interfaces and abstract classes can be used to generalize common features. F How do you decide whether to use an interface or a class?

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved A strong is-an-extension-of relationship F In general, a strong is-an-extension-of relationship that clearly describes a parent- child relationship should be modeled using classes. F For example, since an orange is a fruit, their relationship should be modeled using class inheritance. 17

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved A weak is-an-extension-of relationship F A weak is-an-extension-of relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. F A weak is-an-extension-of relationship can be modeled using interfaces.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Comparable F For example, all strings are comparable, so the String class implements the Comparable interface. F A circle or a rectangle is a geometric object, for example, so Circle can be designed as a subclass of GeometricObject. F Circles are different and comparable based on their radius, for example, so Circle can implement the Comparable interface. 19

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Using Interfaces or Abstract Classes, cont. F Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass, but implement any number of interfaces. F However, interfaces cannot contain concrete methods. F You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface. F So you can use the interface or its companion class whichever is more convenient.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Sequence diagrams Sequence diagrams describe interactions among objects by depicting the time ordering of method invocations.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Sequence diagrams, cont.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Statechart diagrams Statechart diagrams describe flow of control of the object.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Statechart diagrams, cont.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Supplement P: Designing Generic Matrix Classes F Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. GenericMatrix

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Example 12.3, cont.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Example 12.3, cont. F Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. TestIntegerMatrix Run TestRationalMatrix Run RationalMatrix IntegerMatrix

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The Java API F The Java API (Application Program Interface, Application Programming Interface, or Application Programmer interface) consists of numerous classes and interfaces grouped into more than a dozen of packages. F You have used classes and interfaces in the java.lang, javax.swing, and java.util packages.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Framework-Based Programming F To create comprehensive projects, use more classes and interfaces in the Java API. F The classes and interfaces in the Java API establish a framework for programmers to develop applications using Java. F For example, the classes and interfaces in the Java GUI API establish a framework for developing GUI programs. F Use these classes and interfaces and follow their conventions and rules to create applications.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Framework-Based Programming, cont. F Once you understand the concept of Java and object-orient programming, the most important lesson from now on is learning how to use the API to develop useful programs. F The most effective way to achieve it is to imitate good examples.