1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
Basic Java Constructs and Data Types – Nuts and Bolts
1 Inheritance Classes and Subclasses Or Extending a Class.
UML and Classes, Objects and Relationships [1]
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Classes and Objects in Java
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
ABC Technology Project
Object Oriented Programming with Java
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 8 Improving the User Interface
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
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.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
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.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 Stack and Heap. 2 “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Definition: context – the.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
“Packages in Java”.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Objects as a programming concept
Inheritance-Basics.
Packages When we name a program , we name it by class name…
Java Basics Packages.
Interfaces.
Java Programming Language
Packages and Interfaces
Interfaces.
PACKAGES.
Interfaces,Packages and Threads
Presentation transcript:

1 Packages: Putting Classes Together

2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic form limited to reusing the classes within a program. What if we need to use classes from other programs without physically copying them into the program under development ? In Java, this is achieved by using what is known as packages, a concept similar to class libraries in other languages.

3 Packages Packages are Javas way of grouping a number of related classes and/or interfaces together into a single unit. That means, packages act as containers for classes. The benefits of organising classes into packages are: The classes contained in the packages of other programs/applications can be reused. In packages classes can be unique compared with classes in other packages. That two classes in two different packages can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name. Classes in packages can be hidden if we dont want other packages to access them. Packages also provide a way for separating design from coding.

4 Java Foundation Packages Java provides a large number of classes groped into different packages based on their functionality. The six foundation Java packages are: java.lang Contains classes for primitive types, strings, math functions, threads, and exception java.util Contains classes such as vectors, hash tables, date etc. java.io Stream classes for I/O java.awt Classes for implementing GUI – windows, buttons, menus etc. java.net Classes for networking java.applet Classes for creating and implementing applets

5 Using System Packages The packages are organised in a hierarchical structure. For example, a package named java contains the package awt, which in turn contains various classes required for implementing GUI (graphical user interface). Graphics Font java Image … awt lang java Package containing lang, awt,.. packages; Can also contain classes. awt Package containing classes Classes containing methods

6 Accessing Classes from Packages There are two ways of accessing the classes stored in packages: Using fully qualified class name java.lang.Math.sqrt(x); Import package and use class name directly. import java.lang.Math Math.sqrt(x); Selected or all classes in packages can be imported: Implicit in all programs: import java.lang.*; package statement(s) must appear first import package.class; import package.*;

7 Creating Packages Java supports a keyword called package for creating user-defined packages. The package statement must be the first statement in a Java source file (except comments and white spaces) followed by one or more classes. Package name is myPackage and classes are considred as part of this package; The code is saved in a file called ClassA.java and located in a directory called myPackage. package myPackage; public class ClassA { // class body } class ClassB { // class body }

8 Creating Sub Packages Classes in one ore more source files can be part of the same packages. As packages in Java are organised hierarchically, sub-packages can be created as follows: package myPackage.Math package myPackage.secondPakage.thirdPackage Store thirdPackage in a subdirectory named myPackage\secondPackage. Store secondPackage and Math class in a subdirectory myPackage.

9 Accessing a Package As indicated earlier, classes in packages can be accessed using a fully qualified name or using a short-cut as long as we import a corresponding package. The general form of importing package is: import package1[.package2][…].classname Example: import myPackage.ClassA; import myPackage.secondPackage All classes/packages from higher-level package can be imported as follows: import myPackage.*;

10 Using a Package Let us store the code listing below in a file named ClassA.java within subdirectory named myPackage within the current directory (say abc). package myPackage; public class ClassA { // class body public void display() { System.out.println("Hello, I am ClassA"); } class ClassB { // class body }

11 Using a Package Within the current directory (abc) store the following code in a file named ClassX.java import myPackage.ClassA; public class ClassX { public static void main(String args[]) { ClassA objA = new ClassA(); objA.display(); }

12 Compiling and Running When ClassX.java is compiled, the compiler compiles it and places.class file in current directly. If.class of ClassA in subdirectory myPackage is not found, it comples ClassA also. Note: It does not include code of ClassA into ClassX When the program ClassX is run, java loader looks for ClassA.class file in a package called myPackage and loads it.

13 Using a Package Let us store the code listing below in a file named ClassA.java within subdirectory named secondPackage within the current directory (say abc). package secondPackage; public class ClassC { // class body public void display() { System.out.println("Hello, I am ClassC"); }

14 Using a Package Within the current directory (abc) store the following code in a file named ClassX.java import myPackage.ClassA; import secondPackage.ClassC; public class ClassY { public static void main(String args[]) { ClassA objA = new ClassA(); ClassC objC = new ClassC(); objA.display(); objC.display(); }

15 Output package % java ClassY Hello, I am ClassA Hello, I am ClassC package %

16 Protection and Packages All classes (or interfaces) accessible to all others in the same package. Class declared public in one package is accessible within another. Non-public class is not Members of a class are accessible from a difference class, as long as they are not private protected members of a class in a package are accessible to subclasses in a different class

17 Visibility - Revisited Public keyword applied to a class, makes it available/visible everywhere. Applied to a method or variable, completely visible. Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class.

18 Visibility Modifiers

19 Adding a Class to a Package Consider an existing package that contains a class called Teacher: This class is stored in Teacher.java file within a directory called pack1. How do we a new public class called Student to this package. package pack1; public class Teacher { // class body }

20 Adding a Class to a Package Define the public class Student and place the package statement before the class definition as follows: Store this in Student.java file under the directory pack1. When the Student.java file is compiled, the class file will be created and stored in the directory pack1. Now, the package pack1 will contain both the classes Teacher and Student. package pack1; public class Student { // class body } class Teacher package pack1; class Student

21 Packages and Name Clashing When packages are developed by different organizations, it is possible that multiple packages will have classes with the same name, leading to name classing. We can import and use these packages like: import pack1.*; import pack2.*; Student student1; // Generates compilation error class Teacher package pack1; class Student package pack2; class Courses

22 Handling Name Clashing In Java, name classing is resolved by accessing classes with the same name in multiple packages by their fully qualified name. Example: import pack1.*; import pack2.*; pack1.Student student1; pack2.Student student2; Teacher teacher1; Courses course1;

23 Extending a Class from Package A new class called Professor can be created by extending the Teacher class defined the package pack1 as follows: import pack1.Teacher; public class Professor extends Teacher { // body of Professor class // It is able to inherit public and protected members, // but not private or default members of Teacher class. }

24 Summary Packages allow grouping of related classes into a single united. Packages are organised in hierarchical structure. Packages handle name classing issues. Packages can be accessed or inherited without actual copy of code to each program.