Chapter 5 Part 2 COSI 11a 2006-11-08 Prepared by Ross Shaull.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Java Classes Using Java Classes Introduction to UML.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Working With Objects Tonga Institute of Higher Education.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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.
Classes, Interfaces and Packages
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 Introduction to Object Oriented Programming Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
Comp1004: Building Better Objects II Encapsulation and Constructors.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
More About Objects and Methods
Object-Oriented Design
More Sophisticated Behavior
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Defining Your Own Classes
Chapter 6 Methods: A Deeper Look
IFS410: Advanced Analysis and Design
Encapsulation and Constructors
Object Oriented Programming
More About Objects and Methods
Applying OO Concepts Using Java
Chapter 8 Classes User-Defined Classes and ADTs
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Functions, Procedures, and Abstraction
C++ Object Oriented 1.
Presentation transcript:

Chapter 5 Part 2 COSI 11a Prepared by Ross Shaull

Objectives for Today Review encapsulation. Discuss the idea of a class interface, and how we use the tools we have learned to create a useful one. Learn about packages. Write more code in GuessWord.java.

Encapsulation We sometimes call it information hiding. Who can give me some intuitions or definitions for encapsulation? Can the tools we’ve learned in chapters 4 and 5 help us create a well encapsulated class? (yes!)

Friendly Interface Put yourselves in the shoes of another programmer who is going to use the class you’ve written. You want to give this programmer an interface to your class that is: –Safe –Easy to understand –Easy to use

Safe Constructors that either… –Provide parameters to set necessary values corresponding to internal state (i.e. instance variables); or, –Set reasonable defaults for internal state not provided as argument to constructor. Private variables and final variables –How do these improve safety?

Easy to Understand Overloaded methods reduce the number of method names that programmers have to remember. Proper return values and parameters make it clear what a method does. Good comments that can be extracted using javadoc.

Easy to Use Reduce keystrokes! Static methods help because you don’t have to instantiate an object to use them.

Why? Programs are most useful when they can be used multiple times, perhaps in contexts you didn’t even think of. For example, the GuessGame class that we are writing might be useful as a component in another, more complicated program (who is familiar with Wheel of Fortune?).

Why? This idea of reusing code is part of the core philosophy behind Object Oriented design (remember that term from the first lecture?). When you keep these principles in mind, your classes become more useful for other programmers. This makes your programs easier for you to understand, too!

Packages Get organized! Keep code in multiple directories. Resolve name clashes. Works hand-in-hand with good OO design to make your classes easier to use for other programmers (and you). Page 384.

Package Declaration package general.utilities; public class DoubleOut { // stuff here… } This file must be stored in a directory “ general/utilities ” general/utilities can be put in a variety of library paths! Remember the CLASSPATH variable?

Package Example (Windows) C:\myjavastuff\libraries\general\utilities\DoubleOut.java CLASSPATH = “C:\myjavastuff\libraries;.” import general.utilities.DoubleOut; public class DoubleOutTest { // … use DoubleOut in here } The “;.” is very important!

Package Example (OS X) /Users/ross/java/lib/general/utilities/DoubleOut.java CLASSPATH = “/Users/ross/java/lib/;.” import general.utilities.DoubleOut; public class DoubleOutTest { // … use DoubleOut in here } The “;.” is here again!

Summary (Ask Questions!) Invoking methods Method parameters Static methods Constructors Information Leakage Testing techniques Packages