Unit-3 Packages and Interfaces (E-Next - In)
Unit-3 Packages and Interfaces (E-Next - In)
Defination:
A Package can be defined as a grouping of related types (classes, interfaces,
enumerations and annotations) providing access protection and namespace
management.
Defining a package:
To create a package simply use the package command/keyword
Java uses file system directories to store packages. For example the .class files for
any classes you declare to be part of mypack must be stored in the directory mypack.
Also it is case sensitive.
You can create a hierarchy of packages, to do so simply s eparate each package name
from the one above it by using a period(.)
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
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.
2) Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
Subpackage in java
Package inside the package is called the subpackage. It should be created to
categorize the package further.
Vehicle.java
package vehicles;
interface Vehicle
{
public void run();
public void speed();
}
Car.java
package vehicles;
public class Car implements Vehicle
{
Output:
Car is running.
Speed of Car: 50 Km/h
Hello World!"
Q.3 Explain the various levels of access protection available for packages & their
implications.
Access Protection in packages:
Packages add another dimension to access control.
Packages act as containers for classes and other subordinate packages.
Classes and packages are means of encapsulating and containing the name space
and scope of the variables and methods.
Packages behaves as containers for classes and other subordinate packages.
Classes act as containers for data and code.
Q.4 How do we achieve multiple inheritance in java? Explain with java program.
Achieve Multiple inheritance in Java:
The functionality of multiple inheritance is achieved by using Interfaces in Java.
Interfaces are designed to support dynamic method resolution at run time.
Using the keyword interface, you can fully abstract a class's interface from i ts
implementation.
It is used to achieve abstraction and multiple inheritance in Java.
That is, using interface, you can specify what a class must do, but not how it does it.
Interfaces are syntactically similar to classes, but they lack instance variables, and
their methods are declared without any body.
Once it is defined, any number of classes can implement an interface. Also, one class
can implement any number of interfaces.
There are mainly three reasons to use interface. They are given below:
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.
An interface is defined much like a class. This is the general form of an interface:
access interface name
{
return-type method-name1 (parameter-list);
return-type method-name2 (parameter-list);
type final-varname1 = value;
type final-varname2 = value;
}
Example Program:
interface MyInterface
{
public void method1();
public void method2();
}
interface Orange
{
void drink();
}
Q.5 Explain any five built-in packages of java also write steps to create a package.
Types of packages
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the
commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
User-defined packages:
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being
the package names.
package myPackage;
{
public void getNames(String s)
{
System.out.println(s);
}
}
Now we can use the MyClass class in our program.
import myPackage.MyClass;
obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage directory since it is a part of the
package.