Unit-3 Packages and Interfaces (E-Next - In)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Unit-3 Packages and Interfaces

Q.1 What is package? Write a program to create a Package?


Q.2 Write a short note on package.
Package in Java:
 Packages are containers for classes.
 Packages is both a naming convention and visibility control mechanism
 Java package is a group of similar types of classes, interfaces and sub-packages.
 Package in java can be categorized in two form, built-in package and user-defined
package.
 There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql
etc.
 Packages are used in Java in order to prevent naming conflicts, to control access, to
make searching/locating and usage of classes, interfaces, enumerations and
annotations easier, etc.
 Since the package creates a new namespace there won't be any name conflicts with
names in other packages. Using packages, it is easier to provide access control and it
is also easier to locate the related classes.

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

This is the general form of package statement:


 package mypack;

 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(.)

The general form of multilevel package is:


 Package pkg1.pkg2.pkg3;

Some of the existing packages in Java are:


 java.lang − bundles the fundamental classes
 java.io − classes for input , output functions are bundled in this package

Advantage of Java Package:


1) Java package is used to categorize the classes and interfaces so that they can be
easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.

Khan S. Alam 1 https://E-next.in


Unit-3 Packages and Interfaces

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.

3) Using fully qualified name


 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.

Subpackage in java
 Package inside the package is called the subpackage. It should be created to
categorize the package further.

Khan S. Alam 2 https://E-next.in


Unit-3 Packages and Interfaces

Simple example of user defined package:


 A package is a mechanism to group the similar type of classes, interfaces and sub-
packages and provide access control. It organizes classes into single unit.

Vehicle.java
package vehicles;
interface Vehicle
{
public void run();
public void speed();
}

Car.java
package vehicles;
public class Car implements Vehicle
{

public void run()


{
System.out.println("Car is running.");
}

public void speed()


{
System.out.println("Speed of Car: 50 Km/h");
}

public static void main(String args[])


{
Car Car = new Car();
Car.run();
Car.speed();
System.out.println("Hello World!");
}
}

Output:
Car is running.
Speed of Car: 50 Km/h
Hello World!"

Khan S. Alam 3 https://E-next.in


Unit-3 Packages and Interfaces

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.

The three access modifiers are:


1. public
2. private
3. protected
 provides a variety of ways to produce many levels of access required by these
categories.

 Anything declared public can be accessed from anywhere.


 Anything declared private cannot be seen outside of its class.
 When a member does not have an explicit access specification, it is visible to
subclasses as well as to other classes in the same package.
 If you want to allow an element to be seen outside your current package, but only to
classes that subclass your class directly, then declare that element protected.
 A non-nested class has only two possible access levels: default and public. When a
class is declared as public, it is accessible by any other code. If a class has default
access, then it can only be accessed by other code within its same package.

Class Members Visibility


 The Java's smallest unit of abstraction is class. Because of the interplay between the
classes and packages, Java addresses the following four categories of visibility for
class members:
1. Subclasses in same package
2. Non-subclasses in same package
3. Subclasses in different packages
4. Classes that are neither in same package nor in subclasses

Khan S. Alam 4 https://E-next.in


Unit-3 Packages and Interfaces

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;
}

Here is an example of an interface definition:


interface Callback
{
void callback (int param);
}

Example Program:
interface MyInterface
{
public void method1();
public void method2();
}

class XYZ implements MyInterface


{

public void method1()


{
System.out.println("implementation of method1");
}

Khan S. Alam 5 https://E-next.in


Unit-3 Packages and Interfaces

public void method2()


{
System.out.println ("implementation of method2");
}

public static void main(String args[])


{
MyInterface obj = new XYZ();
obj. method1();
}
}

Multiple Interface Program:


interface Apple
{
void eat();
}

interface Orange
{
void drink();
}

class fruit implements Apple,Orange


{
public void eat()
{
system.out.println (“An Apple”);
}
public void drink()
{
system.out.println (“An Orange”);
}

public static void main (String args[])


{
fruit f = new fruit();
f.eat();
f.drink();
}
}
Output:
An Apple
An Orange
}

Khan S. Alam 6 https://E-next.in


Unit-3 Packages and Interfaces

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.

Steps of creating User Defined Packages Java and using them.


1. Create a package with a .class file
2. set the classpath from the directory from which you would like to access. It may be in a
different drive and directory. Let us call it as a target directory.
3. Write a program and use the file from the package.

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 class MyClass

Khan S. Alam 7 https://E-next.in


Unit-3 Packages and Interfaces

{
public void getNames(String s)
{
System.out.println(s);
}
}
Now we can use the MyClass class in our program.
import myPackage.MyClass;

public class PrintName


{
public static void main(String args[])
{
String name = "GeeksforGeeks";
MyClass obj = new MyClass();

obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage directory since it is a part of the
package.

Q.6 List the Java API packages.

Khan S. Alam 8 https://E-next.in

You might also like