Java Unit-3.2
Java Unit-3.2
1
Interfaces
• An interface, is a way of describing what classes
should do, without specifying how they should do
it.
4
Ex:-
interface Callback
{
5
Implementing Interfaces
• Once an interface has been defined, one or more classes can
implement that interface.
[implements interface
[,interface...] ]
{
// class-body
6
• If a class implements more than one interface,
the interfaces are separated with a comma.
7
Example class that implements the Callback interface
class Client implements Callback
{ // Implement Callback's interface
public void callback(int p)
{
System.out.println("callback called with " + p);
}
}
Ex:-
class Client implements Callback
{ // Implement Callback's interface
public void callback(int p)
{
System.out.println("callback called with " + p);
}
void nonIfaceMeth()
{
System.out.println("Classes that implement interfaces " +
"may also define other members, too.");
}
}
9
Accessing Implementations Through Interface
References
• Any instance of any class that implements the
interface can be referred by an interface variable.
10
• The following example calls the callback( ) method
via an interface reference variable:
class TestIface
{
public static void main(String args[])
{
Callback c = new Client();
c.callback(42);
}
}
Note : c can be used to access the callback( ) method,
but it cannot be used to access any other members of
the Client class. 11
Partial Implementations
• If a class does not fully implement the methods defined by
the interface, then that class must be declared as abstract.
Ex:-
abstract class Incomplete implements Callback
{
int a, b;
void show()
{ System.out.println(a + " " + b); }
// ...
}
• Any class that inherits Incomplete must implement
callback( ) or be declared abstract itself.
12
Variables in Interfaces
• You can use interfaces to share constants among classes by
simply declaring an interface that contains variables which are
initialized to the desired values.
Ex:-
interface SharedConstants
{
int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
}
13
Methods in Interfaces
• Methods are Just declared.
14
Interfaces Can Be Extended
• One interface can inherit another by use of the keyword
extends.
15
Ex:-An Application using interfaces
interface A public void meth3()
{ {
void meth1();
System.out.println("Implement
void meth2();
meth3().");
}
}
interface B extends A
{
}
void meth3(); class IFExtend
} {
class MyClass implements B public static void main(String arg[])
{ {
public void meth1()
MyClass ob = new MyClass();
{
ob.meth1();
System.out.println("Implement meth1().");
} ob.meth2();
public void meth2() ob.meth3(); O/P:
{ } Implement meth1().
System.out.println("Implement meth2()."); } Implement meth2().
} Implement meth3().
16
Use of implements and extends
keyword
17
Differences between classes and interfaces
18
Use of Interfaces
• It acts as APIs (Application Programming
Interface)
• It means users can implement interfaces in its
own way (depends on the application)
• It is easy to add new features (like members)
to the interfaces
19
Packages
• The main feature of OOP is its ability to support
the reuse of code:
– Using the classes ( directly )
– Extending the classes (via inheritance)
– Extending interfaces
• The features in basic form limited to reusing the
classes within a program
Ex : 1. mypackage
2. mypackage.util
21
2. Choose a directory on your hard drive as the
root of your class library
22
3.Create subdirectories within the package root
directory for your package name
23
4. Add the root directory for your package to
the classpath environment variable
24
5. Save the files for any classes you want to be
in a particular package in the directory for
that package
25
6. Add a package statement at the beginning of
each source file
26
Ex:
package mypackage.util;
public class Sum
{
public int sumInt(int a[])
{
int s=0;
for(int i=0;i<a.length;i++)
{
s = s+a[i];
}
return s;
}
27
}
Contd..
import mypackage.util.*;
class PackageDemo
{
public static void main( String args[])
{
int x[] = {1,2,3,4,5};
Sum s = new Sum();
System.out.println(s.sumInt(x));
}
}
28
• In general, a Java source file can contain any (or all)
of the following four internal parts:
29
Accessing Classes from Packages
• There are two ways of accessing the classes
stored in packages:
30
• Selected or all classes in packages can be
imported:
– import package.ClassName;
– import package.*;
31
Types of Packages
Package
Built-in User-defined
32
Built-In Packages
java
33
Built-in examples
• import java.util.Scanner;
--java.util package contains Scanner class (has
methods nextInt(), next(),…)
• import java.lang.Math;
--java.lang package contains Math class (has
methods sqrt(), floor(), …)
34
User-defined examples
• import mypackage.util.Sum;
--user defined package mypackage.util
contains Sum class
35