Java_UNIT2
Java_UNIT2
2.1 Packages
Built-in package:
Collection of classes & interfaces which are already defined are called as Build-in packages.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
user-defined packages:
The collection of classes and interfaces for which definition is provided by the
developer or programmer are called as user-defined packages.
2
Java Programming 3
Syntax:
package packagename[.subpackage1][.subpackage2]…[.subpackageN];
//save as Simple.java
package mypack;
System.out.println("Welcome to package");
If you are not using any IDE, you need to follow the syntax given below:
Syntax:
3
Java Programming 4
For example
1. javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can
use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc.
If you want to keep the package within the same directory, you can use . (dot).
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
Output:Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents
destination. The . represents the current folder.
CLASS PATH environment variable setting is used by java run time system to know
where to look for package that is created.
package MyPack
4
Java Programming 5
It must simply specify the path to MyPack. For example, in a Windows environment, if
the path to MyPack is
C:\MyPrograms\Java\MyPack
Then the class path to MyPack is
C:\MyPrograms\Java
Example:
package MyPack;
class Demo
{
…….
…….
}
class classpathdemo
{
public static void main(String args[])
{
Demo obj=new Demo();
………
}
}
Next, compile the file. Make sure that the resulting .class file is also in the MyPack directory.
Then, try executing the classpathdemo class, using the following command line:
java MyPack.classpathdemo
Remember, you will need to be in the directory above MyPack when you execute this
command.
5
Java Programming 6
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
1) Using packagename.*
• If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
• The import keyword is used to make the classes and interface of another
package accessible to the current package.
6
Java Programming 7
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
Output:Hello
2) Using packagename.classname
• If you import package.classname then only declared class of this package will
be accessible.
7
Java Programming 8
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
Output:Hello
8
Java Programming 9
• If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified
name every time when you are accessing the class or interface.
• It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
class B
{
public static void main(String args[])
{
pack.A obj = new pack.A();//using fully
qualified name
obj.msg();
}
}
9
Java Programming 10
Output:Hello
If you import a package, all the classes and interface of that package will be imported
excluding the classes and interfaces of the subpackages. Hence, you need to import the
subpackage as well.
Implicit imports give access to all visible types in the type (or package) that precedes the
".*"; types imported in this way never shadow other types.
Explicit imports give access to just the named type; they can shadow other types that
would normally be visible through an implicit import, or through the normal package
visibility rules.
Example
The following example uses implicit imports. This means that it is not clear to a
programmer where the List type on line 5 is imported from.
10
Java Programming 11
There are two types of modifiers in java: access modifier and non-access modifier. The access modifiers
specifies accessibility (scope) of a datamember, method, constructor or class.
1. private
2. default
3. protected
4. public
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.
The private datamembers, method ,contructor are accessible only within class in which their are declared and
out of the class the doesn’t have scope.
Example of private access modifier:
In this example, we have created two classes ‘A’ and ‘Simple’. ‘A’ class contains private data member and
private method. We are accessing these private members from outside the class, so there is compile time error.
class A
{
private int data=40;
private void msg()
{
System.out.println("Hello java");
}
}
public class Simple
{
public static void main(String args[])
{
A obj=new A();
System.out.println(obj.data);//Compile
11 Time Error
obj.msg();//Compile Time Error
}
}
Java Programming 12
class A
{
private A( )
{
}//private constructor
void msg( )
{
System.out.println("Hello java");
}
}
public class Simple
{
public static void main(String args[])
{
A obj=new A( );//Compile Time Error
}
}
12
Java Programming 13
//save by A.java
package pack;
class A
{
void msg( )
{
System.out.println("Hello");
}
}
//save by B.java
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside
the package.
13
Java Programming 14
The protected access modifier can be applied on the data member, method and constructor. It can't be
applied on the class.
package pack;
public class A
{
protected void msg()
{ System.out.println("Hello");
}
}
import pack.*;
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.msg();
}
}
Output:Hello
14
Java Programming 15
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
15
Java Programming 16
Output:Hello
2.2 INTERFACES
(or)
(or)
An interface in java is a blueprint of a class. It has static final constants and abstract methods.
They are mainly three reasons to use interface. They are given below.
Syntax:
public interface NameOfInterface
In other words, Interface fields are public, static and final by default, and methods are public and abstract.
As shown in the figure given below, a class extends another class, an interface extends another interface but a
class implements an interface
17
Java Programming 18
interface MyInterface
{
public void method();
}
class Demo implements MyInterface
{
public void method()
{
System.out.println("Implementation of method");
}
public static void main(String arg[])
{
18
Java Programming 19
Multiple inheritance is not supported through class in java but it is possible by interface, why?
As we have explained in the inheritance chapter, multiple inheritance is not supported in case of class because
of ambiguity. But it is supported in case of interface because there is no ambiguity as implementation is provided
by the implementation class.
For example:
interface Printable
{
void print();
}
interface Showable
{
void show();
}
Output:
Hello
19
Java Programming 20
Welcome
• Abstract class and interface both are used to achieve abstraction where we can declare the abstract
methods.
But there are many differences between abstract class and interface that are given below.
20
Java Programming 21
• An interface which is declared within another interface or class is known as nested interface.
• The nested interfaces are used to group related interfaces so that they can be easy to maintain.
• The nested interface must be referred by the outer interface or class.
• It can't be accessed directly.
• Nested interface must be public if it is declared inside the interface but it can have any access modifier if
declared within the class.
interface interface_name
{
...
public interface nested_interface_name
{
...
}
}
21
Java Programming 22
void show();
void show()
obj.show();
obj.message();
}
22
}
Java Programming 23
Output:
23
Java Programming 24
Output:
Example2:
If class have any members and methods ,then to access this members and methods
• First extend the class and implement the interfaces
So that you will have access to members and methods of class also.
class A
{
void show()
{
System.out.println("Hello");
}
public interface innerinterface
{
public void message();
}
}
class NestedTest extends A implements A.innerinterface
{
public void message()
{
System.out.println("implementation of nestedinterface message method");
}
public static void main(String args[])
{
NestedTest obj=new NestedTest();
obj.show();
obj.message();
}
}
Output:
Hello
24
Java Programming 25
NOTE:
If we define a class inside the interface, java compiler creates a static nested class. Let's see how can we define
a class within the interface:
interface interface_name
{
class class_name
{
…..
}
}
interface MyInterface
{
public void method();
}
class Demo implements MyInterface
{
public void method()
{
System.out.println("Implementation of method");
}
public static void main(String arg[])
{
Demo obj=new Demo();
obj. method();
}
}
25
Java Programming 26
• An interface can also contain variables just as classes but the variable in interface must be of
• Public,static and final access modifiers.
• If the variable are not specified with access specifier then during compilation process compiler will
provide this access specifier.
interface interface_name
{
….
26
Java Programming 27
Interface inheritance
Output:
Hello
Welcome
27