PAGE-10

CHAPTER-3(Packages and Interfaces)                                   BOTTOM

Applying Interface:-

 

To understand the power of interfaces, let’s look at a more practical example of Stack which has two operations push and pop as described below.

No matter how the stack is implemented, the interface to the stack remains the same. That is, the methods push( ) and pop( ) define the interface to the stack independently of the details of the implementation.

Because the interface to a stack is separate from its implementation, it is easy to define a stack interface, leaving it to each implementation to define the specificsFirst, here is the interface that defines an integer stack. Put this in a file called IntStack.java. This interface will be used by both stack implementations..

// Define an integer stack interface.
interface IntStack {
void push(int item); // store an item
int pop(); // retrieve an item
}

The following program creates a class called FixedStack that implements a fixed-length version of an integer stack:


// An implementation of IntStack that uses fixed storage.
class FixedStack implements IntStack {
private int stck[];
private int tos;
// allocate and initialize stack
FixedStack(int size) {
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
public void push(int item) {
if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
public int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}

In the class below we are calling the implenmented methods push and pop of the interface defined above using the object.

class IFTest {
public static void main(String args[]) {
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
// push some numbers onto the stack
for(int i=0; i<5; i++) mystack1.push(i);
for(int i=0; i<8; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}

Variables in Interfaces:-

interface Constants {
double PI_APPROXIMATION = 3.14;
String AREA_UNITS = " sq.cm.";
String LENGTH_UNITS = " cm.";
}
public class Client implements Constants {
public static void main(String[] args) {
double radius = 1.5;
System.out.println("Area of circle is " +
(PI_APPROXIMATION*radius*radius) +
AREA_UNITS); // (1) Direct access.
.
System.out.println("Circumference of circle is " +
(2*Constants.PI_APPROXIMATION*radius) +
Constants.LENGTH_UNITS); // (2) Fully qualified name.
}
}

Output from the program:

Area of circle is 7.0649999999999995 sq.cm.
Circumference of circle is 9.42 cm.

Now we will see how java provides multiple inheritance:-

java provides multiple inheritance with the help of interfaces.A class can implements multiple interfaces as opossesd to single inheritance .A class can extend only one class and provides a single inheritance.At a time we can use the features of multiple interfaces but in case of class we can access the features of only one class.

.
interface A {  //First Interface
void meth1();
void meth2();
}

interface B { //Second Interface
void meth3();
}


// This class must implement all of A and B
class MyClass implements A , B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();

ob.meth1();
ob.meth2();
ob.meth3();
}
}

Above a class uses the two interfaces with the help of imlements keyword . Interface A declares one method prototype meth1() whereas interface B declares two method prototypes meth2() and meth3().A class Myclass inherits the members of both the interfaces by using the implements keyword .

class MyClass implements A , B

Now Myclass has provide two interfaces in its header ,it can provide the implementation of methods of both the interfaces.

public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");

 

 

Interfaces Can Be Extended

One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. Following is an example:


// One interface can extend another.
interface A {
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A {
void meth3();
}
// This class must implement all of A and B
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
} }


As an experiment you might want to try removing the implementation for meth1( ) in MyClass. This will cause a compile-time error. As stated earlier, any class that implements an interface must implement all methods defined by that interface, including any that are inherited from other interfaces.

.

Home  |   FeedBack  |   Terms of Use  |   Contact Us  |   Report Error
                                                                            Copyright © 2009 R.M Infotech (P) Ltd.                                             Designed by: Raman