EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Characteristics of OOP Encapsulation chap.5 Inheritance chap.6 Polymorphism chap.7.
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.
Packages and Class Visibility The objectives of this chapter are: To understand logical class groupings using packages To explain the environment variable.
How to Create a Java program CS115 Fall George Koutsogiannakis.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
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,
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.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
1 Lecture 2: Object Oriented Programming in Java.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
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.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
Introduction to Java for C/C++ Programmers Note: Much of the following is taken from Professor Gary Shute's web page:
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
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.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
CS-140 Dick Steflik Lecture 3. Java C++ Interpreted optimized for the internet Runs on virtual ized machine Derived from C++ Good object model Widely.
JAVA MULTIPLE CHOICE QUESTION.
Yanal Alahmad Java Workshop Yanal Alahmad
Packages When we name a program , we name it by class name…
Programming without BlueJ Week 12
Java Basics Packages.
Lecture 2 Objects and Classes
Packages and Interfaces
Classes and Objects.
Applying OO Concepts Using Java
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

EE2E1. JAVA Programming Lecture 3 Java Programs and Packages

Contents Java source files and class files Java source files and class files Packages Packages public/package/private scope public/package/private scope The CLASSPATH environment variable The CLASSPATH environment variable

Java source files and class files Java progams execute on the Java Virtual Machine (JVM) which is usually implemented as an interpreter and executed with the java command Java progams execute on the Java Virtual Machine (JVM) which is usually implemented as an interpreter and executed with the java command Java source code is compiled to bytecode by the compiler executed with the javac command Java source code is compiled to bytecode by the compiler executed with the javac command

public class Test{ public static void main(String[] args) { System.out.println(“Hi!”);} } Java source file Test.java Java compiler Java bytecode Java class file Test.class Java interpreter (JVM) Hi!

The name of the source file (Test.java) matches the name of the public class (Test) and produces a bytecode file Test.class The name of the source file (Test.java) matches the name of the public class (Test) and produces a bytecode file Test.class Only one public class per file is allowed (see later - public/package/private scope) Only one public class per file is allowed (see later - public/package/private scope) A source file can contain any number of classes and compilation produces a.class file for each class in the source file A source file can contain any number of classes and compilation produces a.class file for each class in the source file

Packages Java groups classes into packages Java groups classes into packages The standard Java library is distributed over a number of packages including java.lang, java.util indicating sub-packages The standard Java library is distributed over a number of packages including java.lang, java.util indicating sub-packages  java.lang automatically imported into your programs  Contains a lot of the basic classes we use (String,System etc) We can create our own packages using the package keyword We can create our own packages using the package keyword In the following example, classes A and B are in the package my_package In the following example, classes A and B are in the package my_package

package my_package; class A { // A’s public & private members } class B { // B’s public & private members }

The default package If a source file does not begin with the package statement, the classes contained in the source file reside in the default package If a source file does not begin with the package statement, the classes contained in the source file reside in the default package The java compiler automatically looks in the default package to find classes (see later) The java compiler automatically looks in the default package to find classes (see later) It then searches all imported packages to find the remaining classes (see later) It then searches all imported packages to find the remaining classes (see later)

Importing packages The import statement imports packages by directing the compiler where to look The import statement imports packages by directing the compiler where to look The import statement in a Java program must occur before any class definition The import statement in a Java program must occur before any class definition Either the whole package or a sub-package can be imported Either the whole package or a sub-package can be imported Either all of the classes in a package can be imported or just a particular class from the package Either all of the classes in a package can be imported or just a particular class from the package

import my_package.*;// Imports all of the classes class C { // Can refer directly to classes A & B private A a; private B b; } import my_package.A;// Imports class A only class C { // Can refer directly to class A only private A a; private my_package.B b; }

public/package/private scope Scope is concerned with the visibility of program elements such as classes and members Scope is concerned with the visibility of program elements such as classes and members Both classes and class members (methods or instance fields) can be defined with public, package or private scope Both classes and class members (methods or instance fields) can be defined with public, package or private scope A class with public scope means it is visible outside its containing package A class with public scope means it is visible outside its containing package  Allows Java to find a class (maybe with the help of CLASSPATH (see later)) as it must be in a file with the same name as the public class

A class member with public scope means it is visible anywhere its class is visible A class member with public scope means it is visible anywhere its class is visible A class member with private scope means it is visible only within its encapsulating class A class member with private scope means it is visible only within its encapsulating class A class/class member with package scope means it is visible only inside its containing package A class/class member with package scope means it is visible only inside its containing package

Example 1 package my_package; class A// package scope { // A’s public & private members } public class B// public scope { // B’s public and private members }

class A has no private/public label and thus has, by default, package scope class A has no private/public label and thus has, by default, package scope class B has public scope class B has public scope class A is visible only within the package but class B is visible outside the package also class A is visible only within the package but class B is visible outside the package also

package another_package; import my_package.*; class C { // C’s public & private members // class C ‘knows’ about class B private B b;// OK – class B has public scope }

package my_package; class D { // D’s public & private members // Class D ‘knows’ about classes A and B private B b;// OK – class B has public scope private A a;// OK – class A has package scope }

Example 2 package my_package; class A { int get() { return data; }// package scope public A(int d) { data=d;}// public scope private int data;// private scope } class B { void f() { A a=new A(d);// OK A has package scope A a=new A(d);// OK A has package scope int d=a.get();// OK – get() has package scope int d=a.get();// OK – get() has package scope int d1=a.data;// Error! – data is private int d1=a.data;// Error! – data is private}}

Even though member function A() has public scope, because class A only has package scope, the class and hence all of its member functions are only visible inside the package Even though member function A() has public scope, because class A only has package scope, the class and hence all of its member functions are only visible inside the package  If member functions are to be publicly visible, the class itself has to have public scope  In general, classes and their members should be given the narrowest possible scope appropriate to their functionality

The CLASSPATH environment variable The compiler and runtime interpreter know how to find standard packages such as java.lang and java.util The compiler and runtime interpreter know how to find standard packages such as java.lang and java.util The CLASSPATH environment variable is used to direct the compiler and interpreter to where programmer defined imported packages can be found The CLASSPATH environment variable is used to direct the compiler and interpreter to where programmer defined imported packages can be found  This then allows the public class in the file to be found The CLASSPATH environment variable is an ordered list of directories and files The CLASSPATH environment variable is an ordered list of directories and files

Example CLASSPATH’s Example CLASSPATH’s  Windows  UNIX A jar file is a Java archive file and is a collection of java class files (with the.class extension) A jar file is a Java archive file and is a collection of java class files (with the.class extension) ‘.’ means the current directory. In some Java implementations, this is part of the CLASSPATH by default but normally has to be added ‘.’ means the current directory. In some Java implementations, this is part of the CLASSPATH by default but normally has to be added.;C:\java;C:\java\my_jar_file.jar.; /home/java; /home/java/my_jar_file.jar

The above directories and jar files are searched in the order they appear in the CLASSPATH list for imported packages The above directories and jar files are searched in the order they appear in the CLASSPATH list for imported packages The CLASSPATH list can be overridden with the – classpath option to the compiler : The CLASSPATH list can be overridden with the – classpath option to the compiler : (javac –classpath …) and the interpreter (java –classpath …) All files in a package must be in a sub-directory that matches the full package name All files in a package must be in a sub-directory that matches the full package name  Eg. all files in package my_package.sub-package are in a sub-directory my_package/sub-package (Unix) which branches off one of the directories in the CLASSPATH

// source file “my_package/A.java” package my_package; public class A { int get() { return data; } public A(int d) { data=d;} private int data; } // source file “TestA.java” import my_package.A; public class TestA { public static void main(String[] args){ A a=new A(100); } }

class TestA is in the default package class TestA is in the default package Package/sub-package directory matches the directory/sub-directory structure Package/sub-package directory matches the directory/sub-directory structure Default package Package my_package. (current directory) Package Sub-package./my_package Directory Sub-directory

Example We can create a Polygon class which represents a polygon as an array of points (vertices) We can create a Polygon class which represents a polygon as an array of points (vertices) The Point class is imported as part of a (programmer defined) geometrical package geom The Point class is imported as part of a (programmer defined) geometrical package geom A method perimeter() computes the polygon’s perimiter A method perimeter() computes the polygon’s perimiter class PolygonTest contains a main method to test the Polygon class class PolygonTest contains a main method to test the Polygon class

package geom; public class Point { public Point(int x, int y) { xpos=x; ypos=y;} public int getX() {return xpos;} public int getY() { return ypos; } private int xpos, ypos; }

import geom.Point; public class Polygon { public Polygon(int n, Point[] v) { numVertices=n; vertices=new Point[n]; System.arraycopy(v,0,vertices,0,n); System.arraycopy(v,0,vertices,0,n);} public double perimeter() { double pm=0; int nn; double pm=0; int nn; for (int n=1; n<numVertices; n++) for (int n=1; n<numVertices; n++) { int xv1= vertices[n-1].getX(); int yv1=vertices[n-1].getY(); int xv1= vertices[n-1].getX(); int yv1=vertices[n-1].getY(); nn=n%numVertices; nn=n%numVertices; int xv=vertices[nn].getX(); int yv=vertices[nn].getY(); int xv=vertices[nn].getX(); int yv=vertices[nn].getY(); pm+=(Math.sqrt((xv1-xv)*(xv1-xxv)+(yv1-yv)*(yv1-yv))); pm+=(Math.sqrt((xv1-xv)*(xv1-xxv)+(yv1-yv)*(yv1-yv))); } return pm; return pm;} private Point[] vertices; private int numVertices; }

import geom.Point; public class PolygonTest { public static void main(String[] args) { Point[] pointArray={new Point(0,0), new Point(0,1), Point[] pointArray={new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0)}; new Point(1,1), new Point(1,0)}; Polygon square=new Polygon(4,pointArray); Polygon square=new Polygon(4,pointArray); double perim=square.perimeter(); double perim=square.perimeter(); System.out.println(“The perimeter is ” + perim); System.out.println(“The perimeter is ” + perim);}}

class Point is in the geom package whereas classes Polygon and PolygonTest are in the default package class Point is in the geom package whereas classes Polygon and PolygonTest are in the default package File Point.java will reside in a sub-directory geom of the current directory File Point.java will reside in a sub-directory geom of the current directory class Point is made public so it is visible outside of its package class Point is made public so it is visible outside of its package The geom package could be expanded to include other basic geometrical entities (line, curve etc) The geom package could be expanded to include other basic geometrical entities (line, curve etc) Default package Polygon.java PolygonTest.java Sub-package geom Point.java Directory ‘.’ Sub-directory geom

And finally…. When writing seriously large Java applications, you will create many classes and several packages When writing seriously large Java applications, you will create many classes and several packages  Related classes will be grouped into packages When you use a Java API, you normally have to set up the CLASSPATH environment variable so that its classes can be imported When you use a Java API, you normally have to set up the CLASSPATH environment variable so that its classes can be imported  Its important to understand the use of CLASSPATH However, for your lab exercises, keeping all of your public classes in files with the same name as the class in the current directory (default package) will ensure all public classes are visible However, for your lab exercises, keeping all of your public classes in files with the same name as the class in the current directory (default package) will ensure all public classes are visible