Interface and packages
interface
• Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only method
signature, no body).
• If a class implements an interface and does not provide method bodies
for all functions specified in the interface, then the class must be declared
abstract.
• An interface is declared by using the interface keyword.
• All the methods in an interface are declared with the empty body, and all
the fields are public, static and final by default.
• The Java compiler adds public and abstract keywords before the
interface method. Moreover, it adds public, static and final keywords
before data members.
• A class that implements an interface must implement all the methods
declared in the interface.
•Like abstract classes, interfaces cannot be used to create objects
•Interface methods do not have a body - the body is provided by the "implement" class
•On implementation of an interface, you must override all of its methods
•Interface methods are by default abstract and public
•Interface attributes are by default public, static and final
•An interface cannot contain a constructor (as it cannot be used to create objects)
Accessing Implementations Through Interface References:
Multiple inheritance is not supported through class in java, but it is possible by an interface
multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an
interface because there is no ambiguity. It is because its implementation is provided by the implementation class.
packages
• A java package is a group of similar types of classes, interfaces and sub-
packages.
• We can assume package as a folder or a directory that is used to store
similar files.
• Package in java can be categorized in two forms:
• built-in packages:math, util, lang, i/o etc are the example of built-in packages.
• user-defined packages:Java package created by user to categorize their project's
classes and interface are known as user-defined packages.
• 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.
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, String, StringBuffer, Thread). 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,Scanner.
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.
To use a class or a package from the library, you need to use the import keyword:
User-defined packages:
These are the packages that are defined by the user.
How to Create a user defined package:
•Choose the name of the package
•Include the package command as the first line of code in your Java Source File.
•The Source file contains the classes, interfaces, etc you want to include in the package
•Compile to create the Java packages
package p1;
class c1
{
public void m1()
{
System.out.println("m1 of c1");
}
public static void main(string
args[])
{
c1 obj = new c1();
obj.m1();
}
}
1.Save the file as c1.java into the folder d:\ECE
now the file is at location d:\ECE\c1.java
2. Go to command prompt then Compile and create package
D:\ECE>javac –d . c1.java
The above command forces the compiler to create a package in the current working directory.
-d means create a package(directory)
. means it creates a package p1 in the current working directory ie., d:\ECE and place the class file in d:\ECE\p1
D:\ECE\p1\c1.class
D:\ECE> javac –d .. C1.java
The above command creates a package in the parent working directory.
D:\p1\c1.class
D:\ECE>javac –d . c1.java
D:\ECE\p1\p2\c1.class
Instead of . We can also specify the path where we want to create a package.
3. Run the program: d:\ECE> java p1.p2.c1
How to Import Package
1.Save the file with name c3.java in D:\ECE
2.Compile the program
D:\ECE>javac –d . C3.java
3.Create package at d:\ECE\p3\c3.class
4. Run the program
D:\ECE> java p3.c3