Package & Java Access Specifiers

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Java Implementation, Part 2S CompSci 230 Software Construction.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Java Implementation: Part 2 Version 1.4 of : rearranged slides CompSci 230 Software Construction.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
Object-Based Programming Outline Introduction Implementing a Time Abstract Data Type with a Class Class Scope Controlling Access to Members Creating Packages.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
 2002 Prentice Hall. All rights reserved. Chapter 8 – Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 1: Course overview.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
Packages & Binding CompSci 230 S Software Design and Construction.
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.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
By Waqas The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
 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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Packages. Creating and Using Packages To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction 26.2Implementing a Time Abstract Data.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations.
Classes, Interfaces and Packages
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.
/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]
Component-Based Software Engineering Java with added Swing Paul J Krause.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 – Object-Based Programming Part II 8.9 Composition 8.10 Garbage Collection 8.11 Static Class.
CompSci 230 S Programming Techniques
CompSci 230 S Programming Techniques
Chapter 15 Abstract Classes and Interfaces
Software Construction
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Packages When we name a program , we name it by class name…
Lecture 7 Designing Classes
EEC 484 Computer Networks Java Tutorial #1 Wenbing Zhao
Packages Written part of final exam Alex Rudniy
IFS410: Advanced Analysis and Design
Packages, a method of subdividing a Java program and grouping classes
Packages From Deitel & Deitel.
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
CS360 Client/Server Programming Using Java
Presentation transcript:

Package & Java Access Specifiers Chapter 5 Hiding the Implementation Package & Java Access Specifiers

// Graphics.java package graphics; public abstract class Graphic { … } // Circle.java public class Circle extends Graphic implements Draggable {

// Rectangle.java package graphics; public class Rectangle extends Graphic implements Draggable { … } // Draggable.java public interface Draggable {

Creating and Using Packages // Graphics.java package graphics; public abstract class Graphic { … } // Circle.java public class Circle extends Graphic implements Draggable { // Rectangle.java public class Rectangle extends Graphic implements Draggable { // Draggable.java public interface Draggable { Creating and Using Packages

You should bundle these classes and the interface in a package for several reasons: 1、You and other programmers can easily determine that these classes and interfaces are related. 2、You and other programmers know where to find classes and interfaces that provide graphics-related functions.

3、The names of your classes won’t conflict with class names in other packages, because the package creates a new namespace. 4、You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package.

Definition:  A package is a collection of related classes and interfaces providing access protection and namespace management.

Naming a Package The fully qualified name: graphics包: Rectangle 和 java.awt包 :Rectangle 不冲突 The fully qualified name: graphics.Rectangle 和 java.awt.Rectangle they are in different packages, and the fully qualified name of each class includes the package name. That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle.

com.company.region.package. cn.edu.jmu.timer.Time Naming a Package By Convention:  Companies use their reversed Internet domain name in their package names, for example, com.company.region.package. cn.edu.jmu.timer.Time

Using Package Members Refer to the member by its long (qualified) name Only public package members are accessible outside the package in which they are defined. To use a public package member from outside its package, you must do one or more of the following: Refer to the member by its long (qualified) name Import the package member Import the members entire package import cn.edu.jmu.javaprogs.Time1; import cn.edu.jmu.javaprogs.*; import java.awt.*; import java.awt.event.*;

Managing Source and Class Files class name graphics.Rectangle pathname to file graphics/Rectangle.java

You could arrange your source and class directories separately , as shown below:

Classpath参数 Definition:  A class path is an ordered list of directories or ZIP files in which to search for class files

set classpath = D:\javaSample\class;

Time.java: package cn.edu.jmu.timer; 将Time类放入包cn.edu.jmu.timer中, 源文件Time.java存放在与包相对应的目录结构下。 TestTime.java: import cn.edu.jmu.timer.Time; 导入Time类。

// Time.java package cn.edu.jmu.timer; import java.text.DecimalFormat; //用于数据格式化 // 24小时制时间 public class Time extends Object { private int hour; // 0 ~ 23 private int minute; // 0 ~ 59 private int second; // 0 ~ 59 // Time类构造器,初始化每个实例对象的值为0 // 确保每个Time对象处于一致的状态 public Time() { setTime( 0, 0, 0 ); } // 数据的正确性验证,对无效数据取0值 public void setTime( int h, int m, int s ) hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 );

//将通用时间格式转换为字符串 public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } //将标准时间格式转换为字符串 public String toString() return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" );

// TestTime.java // TestTime类需要导入Time类 import javax.swing.JOptionPane; import cn.edu.jmu.timer.Time; // import语句导入Time类 public class TestTime { public static void main( String args[] ) { Time t = new Time(); t.setTime( 13, 27, 06 ); String output = "Universal time is: " + t.toUniversalString() + "\nStandard time is: " + t.toString(); JOptionPane.showMessageDialog( null, output, "Packaging Class Time1 for Reuse", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); }

Attention: Reading Thinking in Java chapter 5