Packages When we name a program , we name it by class name…

Slides:



Advertisements
Similar presentations
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)
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
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.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
Interface 1. Using interface, we specify what a class must do, but not how it does this. An interface is syntactically similar to a class, but it lacks.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Packages and Interfaces 1. Packages The mechanism provided by Java for partitioning the class namespace into more manageable chunks. It is both a naming.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
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.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 2
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Modern Programming Tools And Techniques-I
“Packages in Java”.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Java Basics Packages.
Packages, Interfaces & Exception Handling
Packages and Interfaces 1. Packages The mechanism provided by Java for partitioning the class namespace into more manageable chunks. It is both a naming.
Interfaces.
Abstract Data Types and Encapsulation Concepts
UNIT-3.
Packages Written part of final exam Alex Rudniy
Interface.
Java Programming Language
Packages and Interfaces
Interfaces.
Classes and Objects.
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Applying OO Concepts Using Java
PACKAGES.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Interfaces,Packages and Threads
Presentation transcript:

Packages When we name a program , we name it by class name… Now, how many different names do we need and how difficult to remember them… If we could put them separated in different folders things would become easy !! For it packages are used ….. 7/16/2018 SOBUZ

Packages Provides a mechanism for grouping a variety of classes and / or interfaces together. Grouping is based on functionality. Benefits: The classes contained in the packages of other programs can be reused. In packages, classes can be unique compared with classes in other packages. Packages provides a way to hide classes.

Packages Two types of packages: 1. Java API packages 2. User defined packages Java API Packages: A large number of classes grouped into different packages based on functionality. Examples: 1. java.lang 2. java.util 3. java.io 4. java.awt 5.java.net 6.java. applet etc. 7/16/2018

Package Java Package containing awt package awt Color Package containing classes Graphics Image 7/16/2018

Accessing Classes in a Package Fully Qualified class name: Example:java.awt.Color import packagename.classname; Example: import java.awt.Color; or import packagename.*; Example: import java.awt.*; Import statement must appear at the top of the file, before any class declaration. 7/16/2018

Creating Your Own Package Declare the package at the beginning of a file using the form package packagename; 2. Define the class that is to be put in the package and declare it public(optional) Create a subdirectory under the directory where the main source files are stored (or javac – d . File.java) Store the listing as classname.java in the subdirectory created. Compile the file. From subdirectory. Example: package firstPackage; Public class FirstClass { //Body of the class } 7/16/2018

Alternate way Write your program in your usual directory. Run it with javac –d . File.java This will automatically create a subdirectory having .class file 7/16/2018 SOBUZ

Creating Your Own Package It is just like creating a folder Create a folder put your files in it Compile it from the package directory It is simply that files in different folder can have same name. 7/16/2018

Points !! We can have only one class in Package to be public , and other than public are not accessible outside so what to do. We have to create different files within same package for each public class make different files. Please note that source file and class file of all the files of package should be in package only and not in root directory. Files can be in different directory then to deal with them CLASS PATH come in action. 7/16/2018 SOBUZ

Adding a Class to a Package Every java source file can contain only class declared as public. The name of the source file should be same as the name of the public class with .java extension. package p1; public ClassA{ ……………} Source file : ClassA.java Subdirectory: p1 package p1; public ClassB{…………} Source file: ClassB.java Subdirectory:p1 7/16/2018 S

Example1-Package package p1; public class ClassA { public void displayA( ) System.out.println(“Class A”); } import p1.ClassA; Class testclass { public static void main(String str[]) ClassA obA=new ClassA(); obA.displayA(); } Source file – ClassA.java Subdirectory-p1 ClassA.Java and ClassA.class->p1 Source file-testclass.java testclass.java and testclass.class->in a directory of which p1 is subdirectory. 7/16/2018

Creating Packages Consider the following declaration: package firstPackage.secondPackage; This package is stored in subdirectory named firstPackage.secondPackage. A java package can’t contain more than one class definitions that can be declared as public. Only one of the classes which is declared public and that class name with .java extension is the source file name. 7/16/2018

Example2-Package package p2; public class ClassB { protected int m =10; public void displayB() System.out.println(“Class B”); System.out.println(“m= “+m); } import p1.ClassA; import p2.ClassB; class PackageTest2 { public static void main(String str[]) ClassA obA=new ClassA(); Classb obB=new ClassB(); obA.displayA(); obB.displayB(); } 7/16/2018

Example 3- Package import p2.ClassB; class ClassC extends ClassB { int n=20; void displayC() System.out.println(“Class C”); System.out.println(“m= “+m); System.out.println(“n= “+n); } class PackageTest3 { public static void main(String args[]) ClassC obC = new ClassC(); obC.displayB(); obC.displayC(); } 7/16/2018

Package Correct Code: import p1.*; import p2.*; p1.Student student1; package p1; public class Teacher {………….} public class Student {……………..} In Different files package p2; public class Courses {………..} {………………..}..Different files import p1.*; import p2.*; Student student1; //Error Correct Code: import p1.*; import p2.*; p1.Student student1; p2.Student student2; 7/16/2018

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. 7/16/2018

Finding Packages Two ways: 1.By default, java runtime system uses current directory as starting point and search all the subdirectories for the package. 2.Specify a directory path using CLASSPATH environmental variable. 7/16/2018

CLASSPATH Environment Variable 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 an ordered list of directories and files 7/16/2018

CLASSPATH Environment Variable To set the CLASSPATH variable we use the following command: set CLASSPATH=c:\ Java compiler and interpreter searches the user defined packages from the above directory. To clear the previous setting we use: set CLASSPATH= 7/16/2018 SOBUZ

Example1-Package[Using CLASSPATH] package p1; public class ClassA { public void displayA( ) System.out.println(“Class A”); } import p1.ClassA; Class PackageTest1 { public static void main(String str[]) ClassA obA=new ClassA(); obA.displayA(); }} Source file – c:\p1\ClassA.java Compile-javac c:\p1\ClassA.java Class file in –c:\p1\ClassA.class Source file-c:\java\jdk1.6.0_06\bin\PackageTest1.java Compile-javac PackageTest1.java Copy –PackageTest1.class -> c:\ Execute-java PackageTest1 7/16/2018 SOBUZ

Example2-Package[Using CLASSPATH] package p2; public class ClassB { protected int m =10; public void displayB() System.out.println(“Class B”); System.out.println(“m= “+m); } import p1.*; import p2.*; class PackageTest2 { public static void main(String str[]) ClassA obA=new ClassA(); Classb obB=new ClassB(); obA.displayA(); obB.displayB();} } Source file-c:\java\jdk1.6.0_06\bin\PackageTest2.java Compile-javac PackageTest2.java Copy –PackageTest2.class -> c:\ Execute-java PackageTest2 Source file – c:\p2\ClassB.java Compile-c:\p2\ClassB.java Class file in –c:\p2\ClassB.class 7/16/2018 SOBUZ

Example 3- Package[Using CLASSPATH] import p2.ClassB; class ClassC extends ClassB { int n=20; void displayC() System.out.println(“Class C”); System.out.println(“m= “+m); System.out.println(“n= “+n); } class PackageTest3 { public static void main(String args[]) ClassC obC = new ClassC(); obC.displayB(); obC.displayC(); } Source file-c:\java\jdk1.6.0_06\bin\PackageTest3.java Compile-javac PackageTest3.java Copy –PackageTest3.class -> c:\ Execute-java PackageTest3 Source file – c:\ClassC.java Compile-c:\ClassC.java Class file in –c:\ClassC.class 7/16/2018 SOBUZ

Adding a Class to a Package Every java source file can contain only class declared as public. The name of the source file should be same as the name of the public class with .java extension. package p1; public ClassA{ ……………} Source file : ClassA.java Subdirectory: p1 package p1; public ClassB{…………} Source file: ClassB.java Subdirectory:p1 7/16/2018 S

Adding a Class to a Package Decide the name of the package. Create the subdirectory with this name under the directory where the main source file is located. Create classes to be placed in the package in separate source files and declare the package statement package packagename; 4. Compile each source file. When completed the package will contain .class files of the source files. 7/16/2018

public/package/private scope Scope is concerned with the visibility of program elements such as classes and members Class members (methods or instance fields) can be defined with public, package (default), private or protected scope A class has two levels of visibility: -public scope means it is visible outside its containing package - default scope means it is visible only inside the package. (package scope/ friendly scope) 7/16/2018

public/package/private scope 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/class member with package scope means it is visible only inside its containing package A class member with protected scope means it is visible every where except the non-subclasses in other package. 7/16/2018

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 7/16/2018

Example-2 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 } 7/16/2018

Example-3 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 } 7/16/2018

Example 4 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 int d=a.get(); // OK – get() has package scope int d1=a.data; // Error! – data is private 7/16/2018 SOBUZ

Levels of Access Control public protected friendly (default) private same class Yes Subclass in the same package No Other class in the same package Subclass in other packages Non-subclass in other package 7/16/2018

Interface Similar to a class. Consists of only abstract methods and final variables. Any number of classes can implement an interface. One class can implement any number of interfaces. To implement an interface a class must define each of the method declared in the interface. Each class can also add new features. Interface disconnect the definition of a method or set of methods from the inheritance hierarchy. 7/16/2018

Defining an Interface General form of an interface: access interface name { ret-type method1(parameter list); ret-type method2(parameter list); type final var1 = value; type final static val2 = value; } Example: interface callback{ void callback (int param); } 7/16/2018

Defining an Interface Access is either public or default. Variables declared inside an interface are implicitly final and static. Variables must be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public. 7/16/2018

Implementing Interfaces The General Form: access class classname [extends superclass][implements interface[,interface]] { } The methods that implement an interface must be declared public. The type signature of the implementing method must match exactly the type signature specified in the interface definition. 7/16/2018

Accessing Implementations through Interface Reference Interface reference is required to access the implementation. Any instance of the class that implements the interface can be referred to by such a variable. When a method is called through one of the reference, the correct version will be called based on the actual instance of the interface being referred to. The method to be executed is looked up dynamically at run time. 7/16/2018

Example-1 interface call { void callback(int param); } class client implements call public void callback(int p) System.out.println("callback called with "+p); public class testIface public static void main(String args[]) call c = new client(); c.callback(423); 7/16/2018

Example-2 public class testIface { interface call { void callback(int param); } class client implements call public void callback(int p) System.out.println("callback is called with "+p); class anotherclient implements call System.out.println("p squred is "+(p*p)); public class testIface { public static void main(String args[]) call c = new client(); c.callback(42); c=new anotherclient(); c.callback(10); } 7/16/2018

Partial Implementation If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract. Example: abstract class temp implements call{ int a, b; void show() { //body of the method } Any class that inherits temp must implement callback() or declared abstract itself. 7/16/2018

Extending Interfaces interface Item extends ItemConstant { One interface can inherit another by using the keyword extends. The new sub interface will inherit all the member of the super interface. Any class that will implement the interface that inherits another interface, it must provide implementations of all methods defined within the interface inheritance chain. General form: interface name2 extends name1 { //body of name2 } Example: interface ItemConstant int code =1001; String name =“Pen”; interface Item extends ItemConstant { void display(); } An interface cannot extends a class. 7/16/2018

Multiple Inheritance Using Interface Java supports multiple inheritance through the use of interface. Care should be taken to avoid some conflicts. 7/16/2018

Example-3 interface test1 { int val=10; void display(); } class test3 implements test1, test2 { public void display() System.out.println(“In test3”); System.out.println(test1.val); System.out.println(test2.val); } 7/16/2018

Example-4 interface test1 { int val=10; void display(); } interface test3 extends test1, test2 int val=50; class test4 implements test3 { int val=57; public void display() System.out.println(test1.val); System.out.println(test2.val); System.out.println(test3.val); System.out.println(val); } public class Iface_test public static void main(String args[]) test4 ob = new test4(); ob.display(); 7/16/2018

Example-5 interface test1 { int val=33; void display(); } class test2 implements test1 static int val=34; void display() System.out.println(test1.val); System.out.println(val); class test3 extends test2 { int val=35; void show() System.out.println(test1.val); System.out.println(test2.val); System.out.println(val); } class test4 public static void main(String args[]) test3 ob = new test3(); ob.show(); 7/16/2018