SlideShare a Scribd company logo
1 of 13
Interfaces And Packages
Chapter Four
1
Interfaces and Packages
 Interfaces are java’s substitute for C++ feature of multiple
inheritance. It is the practice of allowing a class to have several
superclasses.
 Java does not allow multiple inheritances.
 Java classes ,however, can implement several interfaces.
 An interface is purely an abstract class, but is different in that
all it contains is empty functions and perhaps static final
constants.
 An interface can’t be instantiated. Its purpose is to specify a
set of requirements for a class to implement.
 These requirements are regarded as a “contract” between the
implementing class and any client class that uses it.
 A class can only inherit a single superclass but can implement
implement more than one interface.2
Interface declaration
[public]<interface><interfaceName>{ //…. }
Example:-interface definition
public interface Shape{
[public abstract] double area();
[public abstract]double volume();
String getName(); }
 An interface is a description of a capability. It lists the methods that a class
must implement to carry out that capability.
 An interface typically consists of declarations of related methods, although an
interface also may contain static and final fields that represent constants.
 In any case, all interface members are public.
 A class that implements an interface typically provides definitions for the
methods declared in the interface.
3
 A class that implements an interface must provide an
implementation for all the method signatures in the interface.
 An interface declaration introduces a new reference type
whose members are constants and abstract methods.
 An interface is typically used in place of an abstract class when
there is no default implementation to inherit. i.e., no instance
variables and no default methods implementations.
 A compile-time error occurs if the identifier naming an
interface appears as the name of any other class or interface in
the same package.
 All interface methods are abstract regardless of whether the
term abstract occurs. an interface is abstract because an
interface can’t be instantiated as an object.
 In Java it is possible for an interface to extend more than one
base interface.
4
Example:-Interface implementation
public interface Shape{
public double getArea();
public double getVolume();
public String getName();
}//end interface
public class Point extends Object implements Shape{
private int xCoord, yCoord;
public Point(){ }
public Point(int x, int y){ xCoord=x; yCoord=y; }
public int getX(){ return xCoord; }
public int getY(){ return yCoord; }
public String toString(){ return “point = (“+getX()+”,”+ getY()+”)”;
}
public double getArea(){ return 0.0; }
public double getVolume(){ return 0.0;}
public double getName(){ return “Point”;}; }5
Overridden
Methods
Interface inheritance
 In Java a class can be derived only from one base class. That is the following
declarations are not allowed.
class C extends A,B{// …. body }// wrong !!
 But an interface can extend multiple interfaces and a class can implement
multiple interfaces. Refer the following interface inheritance diagram.
6
Soakable
Scrubabl
e
BubbleBathab
le
Based on the diagram given in the previous slide, you can
declare the following interfaces.
public interface Washable{
void wash();
}
public interface Soakable extends Washable{
void soak();
}
public interface Scrubable extends Washable{
void Scrubable();
}
public interface BubbleBathable extends
Soakable,Scrubable{
void takeBubbleBath();
}
7
A class can implement several interfaces.
public class CoffeCup implements BubbleBathable,
Breakable{
void wash(){ // }
void soak(){ // }
void scrub(){ // }
void takeBubbleBath(){ // }
void breakIt(){ // }
}
Any class that implements a sub interface must override (implement )
all the abstract methods found in the sub-interface or in the ancestors
of the sub-interface
8
PACKAGES
 Packages are containers for classes that are used to keep the class name
space compartmentalized.
 A package contains a set of related classes.
 To create a package, simply include a package command as the first
statement in a Java source file. Any classes declared within that file will
belong to the specified package. The package statement defines a name
space in which classes are stored. If you omit the package statement, the
class names are put into the default package.
 Syntax:
package packageName;
 A package name consists of one or more identifiers separated by periods.
there is a special package, called the default package, which has no name.
If you did not include any package statement at the top of your source
file, its classes are placed in the default package.
9
If you want to use a class from a package, you can refer to it by its full
name(package name plus class name). For example, java.io.*; refers
to all the class in the java.io package:
The ‘import’ directive lets you refer to a class of a package by its class
name, without the.
You cannot rename a package prefix without renaming the directory in
which the classes are stored.
A package name corresponds to the directory name where the file is
located.
Remember that case is significant, and the directory name must
match the package name exactly.
By default the java.lang.*; package will be imported.
10
Contd...
 The syntax for a multileveled package statement is:
package pkg1[.pkg2[.pkg3]];
 A package is located in a subdirectory that matches the
name. The parts of the name between periods represent
successively nested directories.
 For example, the package amu.univ.it.project.bigjava would
be placed in a subdirectory amu/univ/it/project/bigjava.
 In a large project, it is inevitable that two people will come up with
the same name for the same concept. It is important to avoid such
name clashes. To alleviate such problem use a domain name in
reverse to construct unambiguous package names.
 Classes and packages are both means of encapsulating and
containing the name space and scope of variables and methods.
 Packages act as containers for classes and other subordinate
packages.
11
Creating and using packages
 If you want to make a package of a group of classes, called say myPack,
do the following:
1. Create a folder called myPack and place the .java files into the folder.
2. Create Java file(s) and save the file(s) within myPack folder
 Note that each class within the package begins with package
myPack;
3. Compile the java file(s).
4. Write the main class which imports the package and save it in the
myPack folder.
 The main class has to import the package like import myPack.*;
5. Compile the main class
6. Run the main class12
Access Modifiers
Where
Members are found
in?
Access modifiers type
Private Default protecte
d
public
Same class yes Yes Yes yes
Same package
subclass
No yes Yes Yes
Same package
non-subclass
No Yes yes Yes
Different package
subclass
No No Yes Yes
Different package
non-subclass
No No no Yes
13

More Related Content

What's hot (20)

Fundamentals of oops in .Net
Fundamentals of oops in .NetFundamentals of oops in .Net
Fundamentals of oops in .Net
 
Classboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methodsClassboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methods
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
C plusplus
C plusplusC plusplus
C plusplus
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
 
JavaFX In Practice
JavaFX In PracticeJavaFX In Practice
JavaFX In Practice
 
7494610
74946107494610
7494610
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Class and object
Class and objectClass and object
Class and object
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 

Similar to Z blue interfaces and packages (37129912)

Similar to Z blue interfaces and packages (37129912) (20)

Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Java packages
Java packagesJava packages
Java packages
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Packages in java
Packages in javaPackages in java
Packages in java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Java packages
Java packagesJava packages
Java packages
 
Java 06
Java 06Java 06
Java 06
 
javapackage
javapackagejavapackage
javapackage
 
Java package
Java packageJava package
Java package
 
Packages
PackagesPackages
Packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package in Java
Package in JavaPackage in Java
Package in Java
 

More from Narayana Swamy

More from Narayana Swamy (7)

AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINESAICTE GUIDLINES  AICTE GUIDLINESAICTE GUIDLINES
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
 
Files io
Files ioFiles io
Files io
 
Exceptions
ExceptionsExceptions
Exceptions
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Exceptions
ExceptionsExceptions
Exceptions
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

Z blue interfaces and packages (37129912)

  • 2. Interfaces and Packages  Interfaces are java’s substitute for C++ feature of multiple inheritance. It is the practice of allowing a class to have several superclasses.  Java does not allow multiple inheritances.  Java classes ,however, can implement several interfaces.  An interface is purely an abstract class, but is different in that all it contains is empty functions and perhaps static final constants.  An interface can’t be instantiated. Its purpose is to specify a set of requirements for a class to implement.  These requirements are regarded as a “contract” between the implementing class and any client class that uses it.  A class can only inherit a single superclass but can implement implement more than one interface.2
  • 3. Interface declaration [public]<interface><interfaceName>{ //…. } Example:-interface definition public interface Shape{ [public abstract] double area(); [public abstract]double volume(); String getName(); }  An interface is a description of a capability. It lists the methods that a class must implement to carry out that capability.  An interface typically consists of declarations of related methods, although an interface also may contain static and final fields that represent constants.  In any case, all interface members are public.  A class that implements an interface typically provides definitions for the methods declared in the interface. 3
  • 4.  A class that implements an interface must provide an implementation for all the method signatures in the interface.  An interface declaration introduces a new reference type whose members are constants and abstract methods.  An interface is typically used in place of an abstract class when there is no default implementation to inherit. i.e., no instance variables and no default methods implementations.  A compile-time error occurs if the identifier naming an interface appears as the name of any other class or interface in the same package.  All interface methods are abstract regardless of whether the term abstract occurs. an interface is abstract because an interface can’t be instantiated as an object.  In Java it is possible for an interface to extend more than one base interface. 4
  • 5. Example:-Interface implementation public interface Shape{ public double getArea(); public double getVolume(); public String getName(); }//end interface public class Point extends Object implements Shape{ private int xCoord, yCoord; public Point(){ } public Point(int x, int y){ xCoord=x; yCoord=y; } public int getX(){ return xCoord; } public int getY(){ return yCoord; } public String toString(){ return “point = (“+getX()+”,”+ getY()+”)”; } public double getArea(){ return 0.0; } public double getVolume(){ return 0.0;} public double getName(){ return “Point”;}; }5 Overridden Methods
  • 6. Interface inheritance  In Java a class can be derived only from one base class. That is the following declarations are not allowed. class C extends A,B{// …. body }// wrong !!  But an interface can extend multiple interfaces and a class can implement multiple interfaces. Refer the following interface inheritance diagram. 6 Soakable Scrubabl e BubbleBathab le
  • 7. Based on the diagram given in the previous slide, you can declare the following interfaces. public interface Washable{ void wash(); } public interface Soakable extends Washable{ void soak(); } public interface Scrubable extends Washable{ void Scrubable(); } public interface BubbleBathable extends Soakable,Scrubable{ void takeBubbleBath(); } 7
  • 8. A class can implement several interfaces. public class CoffeCup implements BubbleBathable, Breakable{ void wash(){ // } void soak(){ // } void scrub(){ // } void takeBubbleBath(){ // } void breakIt(){ // } } Any class that implements a sub interface must override (implement ) all the abstract methods found in the sub-interface or in the ancestors of the sub-interface 8
  • 9. PACKAGES  Packages are containers for classes that are used to keep the class name space compartmentalized.  A package contains a set of related classes.  To create a package, simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package.  Syntax: package packageName;  A package name consists of one or more identifiers separated by periods. there is a special package, called the default package, which has no name. If you did not include any package statement at the top of your source file, its classes are placed in the default package. 9
  • 10. If you want to use a class from a package, you can refer to it by its full name(package name plus class name). For example, java.io.*; refers to all the class in the java.io package: The ‘import’ directive lets you refer to a class of a package by its class name, without the. You cannot rename a package prefix without renaming the directory in which the classes are stored. A package name corresponds to the directory name where the file is located. Remember that case is significant, and the directory name must match the package name exactly. By default the java.lang.*; package will be imported. 10 Contd...
  • 11.  The syntax for a multileveled package statement is: package pkg1[.pkg2[.pkg3]];  A package is located in a subdirectory that matches the name. The parts of the name between periods represent successively nested directories.  For example, the package amu.univ.it.project.bigjava would be placed in a subdirectory amu/univ/it/project/bigjava.  In a large project, it is inevitable that two people will come up with the same name for the same concept. It is important to avoid such name clashes. To alleviate such problem use a domain name in reverse to construct unambiguous package names.  Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods.  Packages act as containers for classes and other subordinate packages. 11
  • 12. Creating and using packages  If you want to make a package of a group of classes, called say myPack, do the following: 1. Create a folder called myPack and place the .java files into the folder. 2. Create Java file(s) and save the file(s) within myPack folder  Note that each class within the package begins with package myPack; 3. Compile the java file(s). 4. Write the main class which imports the package and save it in the myPack folder.  The main class has to import the package like import myPack.*; 5. Compile the main class 6. Run the main class12
  • 13. Access Modifiers Where Members are found in? Access modifiers type Private Default protecte d public Same class yes Yes Yes yes Same package subclass No yes Yes Yes Same package non-subclass No Yes yes Yes Different package subclass No No Yes Yes Different package non-subclass No No no Yes 13