MODULE-03 (Interfaces and Packages)
MODULE-03 (Interfaces and Packages)
MODULE-03
INTERFACES AND PACKAGES: Interface: Interfaces VS Abstract classes, defining an interface,
implement interfaces, accessing implementations through interface references, extending interface;
Packages: Defining, creating and accessing a package, understanding CLASSPATH, importing
packages.
INTERFACE:-
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance
in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
There are mainly three reasons to use interface. They are given below.
//Creating abstract class that provides the implementation of one method of A interface
abstract class B implements A
{
public void c(){System.out.println("I am C");
}
}
//Creating subclass of abstract class, now we need to provide the implementation of rest of the methods
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
void print();
interface Showable
void show();
System.out.println("Hello");
System.out.println("Welcome");
obj.print();
obj.show();
OUTUT: Hello
Welcome
We can declare variables as object references that use an interface rather than a class.
Any instance that implements the declared interface can be referred to by such a variable.
When you call a method through one of these references, the correct version will be called
based on the actual instance of the interface being referred to.
This process is similar to using a superclass reference to access a subclass object.
The following example calls the callback() method via an interface reference variable:
Example:
interface Callback {
void callback(int param);
}
class Client implements Callback {
The variable c is declared to be of the interface type Callback, yet it was assigned an instance of
Client.
Although c can be used to access the callback() method, it cannot access any other members of the
Client class.
An interface reference variable has knowledge only of the methods declared by its interface
declaration.
Thus, c could not be used to access aa() since it is defined by Client but not Callback
Extending interface:
An interface contains variables and methods like a class but the methods in an interface are abstract
by default unlike a class. An interface extends another interface like a class implements an interface
in interface inheritance.
interface B extends A {
void funcB();
}
class C implements B {
public void funcA() {
System.out.println("This is funcA");
}
public void funcB() {
System.out.println("This is funcB");
}
}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
}
}
OUTUT: This is funcA
This is funcB
Explanation:
Now let us understand the above program.
The interface A has an abstract method funcA(). The interface B extends the interface A and has an
abstract method funcB(). The class C implements the interface B. A code snippet which demonstrates
this is as follows:
interface A {
void funcA();
}
interface B extends A {
void funcB();
}
class C implements B {
public void funcA() {
System.out.println("This is funcA");
}
public void funcB() {
System.out.println("This is funcB");
}
}
In the method main() in class Demo, an object obj of class C is created. Then the methods funcA()
and funcB() are called. A code snippet which demonstrates this is as follows:
Packages:
A 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.
Here, we will have the detailed learning of creating and using user-defined packages.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the
java.lang: Contains language support classes(e.g classes which defines primitive data types,
java.util: Contains utility classes which implement data structures like Linked List, Dictionary
java.awt: Contain classes for implementing the components for graphical user interfaces (like
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
obj.getNames(name);
}
}
//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:
For example
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
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 .
There are three ways to access the package from outside the package.
import package.*;
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.
//save by A.java
package pack;
public class A{
System.out.println("Hello");
//save by B.java
package mypack;
import pack.*;
class B{
obj.msg();
Output: Hello
2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
import pack.A;
class B{
obj.msg();
Output:Hello
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.
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
class B{
obj.msg();
Output:Hello
Java has an import statement that allows you to import an entire package (as in earlier examples),
For example,
If you want to use class/interface from a certain package, you can also use its fully qualified name,
import java.util.Date;
// body
The same task can be done using the fully qualified name as follows:
//body
Understanding CLASSPATH:
CLASSPATH: A classpath environment variable is the location from which classes are loaded at
runtime by JVM in java.
LOCATION: Locations is directory which may consist of class files or jar files(jar consists of class
files).
ClassNotFoundException is thrown when JVM tries to class from classpath but it does not find that
class.
What is Path? A path is a unique location of a file/ folder in a OS. PATH variable is also called system
variable or environment variable.
JAVA_HOME &
PATH >
variable value: C:\Program Files\Java\jdk1.7.0_51 (Directory in which java has been installed)
Click OK.
Then enter -
Click OK.
open cmd
type
NOTE: Once you will exit cmd JAVA_HOME and PATH will be lost.