Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.

Slides:



Advertisements
Similar presentations
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
Advertisements

 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Inheritance Constructors Dale Roberts, Lecturer Computer Science, IUPUI
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Object Oriented Programming using Java - Polymorphism
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Spring CISC6795: Object Oriented Programming II.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Dale Roberts Object Oriented Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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:
Object Oriented Programming
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Classes and Objects: A Deeper Look
Polymorphism, Interfaces & Operator Overloading
Classes and Objects: A Deeper Look (Chapter 8)
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
Object Based Programming
Chapter 6 Methods: A Deeper Look
IFS410: Advanced Analysis and Design
MSIS 655 Advanced Business Applications Programming
Packages and Interfaces
Packages From Deitel & Deitel.
Object Oriented Programming in java
Chap 1. Getting Started Objectives
Oriented Design and Abstract Data Type
Presentation transcript:

Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts 2 Software Reusability Rapid application development Software reusability speeds the development of powerful, high-quality software Java APIs provides an entire framework in which Java developers can work to achieve true reusability and rapid application development Documentation: java.sun.com/javase/6/docs/api/ Or to download

Dale Roberts 3 Avoid reinventing the wheel. Study the capabilities of the Java API. If the API contains a class that meets your program’s requirements, use that class rather than create your own. Good Programming Practice 8.2

Dale Roberts 4 Data Abstraction and Encapsulation Data abstraction Information hiding Classes normally hide the details of their implementation from their clients Abstract data types (ADTs) Data representation example: primitive type int is an abstract representation of an integer int s are only approximations of integers, can produce arithmetic overflow Operations that can be performed on data

Dale Roberts 5 Software Engineering Observation 8.15 Programmers create types through the class mechanism. New types can be designed to be as convenient to use as the built-in types. This marks Java as an extensible language. Although the language is easy to extend via new types, the programmer cannot alter the base language itself.

Dale Roberts 6 Time Class Case Study: Creating Packages To declare a reusable class Declare a public class Add a package declaration to the source-code file must be the first executable statement in the file package name should consist of your Internet domain name in reverse order followed by other names for the package example: com.deitel.jhtp7.ch08 package name is part of the fully qualified class name Distinguishes between multiple classes with the same name belonging to different packages Prevents name conflict (also called name collision) Class name without package name is the simple name

Dale Roberts 7Outline Time1.java (1 of 2) package declaration Time1 is a public class so it can be used by importers of this package

Dale Roberts 8Outline Time1.java (2 of 2)

Dale Roberts 9 Time Class Case Study: Creating Packages (Cont.) Compile the class so that it is placed in the appropriate package directory structure Example: our package should be in the directory com deitel jhtp7 ch08 javac command-line option –d javac creates appropriate directories based on the class’s package declaration A period (. ) after –d represents the current directory

Dale Roberts 10 Time Class Case Study: Creating Packages (Cont.) Import the reusable class into a program Single-type-import declaration Imports a single class Example: import java.util.Random; Type-import-on-demand declaration Imports all classes in a package Example: import java.util.*;

Dale Roberts 11 Common Programming Error 8.12 Using the import declaration import java. *; causes a compilation error. You must specify the exact name of the package from which you want to import classes.

Dale Roberts 12Outline Time1Packa geTest.java (1 of 2) (1 of 2) Single-type import declaration Refer to the Time1 class by its simple name

Dale Roberts 13Outline Time1P ackageT est.java (2 of 2) (2 of 2)

Dale Roberts 14 Time Class Case Study: Creating Packages (Cont.) Class loader Locates classes that the compiler needs First searches standard Java classes bundled with the JDK Then searches for optional packages These are enabled by Java’s extension mechanism Finally searches the classpath List of directories or archive files separated by directory separators These files normally end with.jar or.zip Standard classes are in the archive file rt.jar

Dale Roberts 15 Time Class Case Study: Creating Packages (Cont.) Time Class Case Study: Creating Packages (Cont.) To use a classpath other than the current directory -classpath option for the javac compiler Set the CLASSPATH environment variable The JVM must locate classes just as the compiler does The java command can use other classpathes by using the same techniques that the javac command uses

Dale Roberts 16 Common Programming Error 8.13 Specifying an explicit classpath eliminates the current directory from the classpath. This prevents classes in the current directory (including packages in the current directory) from loading properly. If classes must be loaded from the current directory, include a dot (. ) in the classpath to specify the current directory.

Dale Roberts 17 Software Engineering Observation 8.16 In general, it is a better practice to use the - classpath option of the compiler, rather than the CLASSPATH environment variable, to specify the classpath for a program. This enables each application to have its own classpath.

Dale Roberts 18 Error-Prevention Tip 8.3 Specifying the classpath with the CLASSPATH environment variable can cause subtle and difficult-to-locate errors in programs that use different versions of the same package.

Dale Roberts 19 Package Access Package access Methods and variables declared without any access modifier are given package access This has no effect if the program consists of one class This does have an effect if the program contains multiple classes from the same package Package-access members can be directly accessed through the appropriate references to objects in other classes belonging to the same package Supports tightly coupled classes that together support package functionality.

Dale Roberts 20Outline PackageData Test.java (1 of 2) (1 of 2) Can directly access package-access members

Dale Roberts 21Outline PackageData Test.java (2 of 2) (2 of 2) Package-access instance variables

Dale Roberts Acknowledgements Deitel, Java How to Program