KEMBAR78
working with interfaces in java programming | PPTX
INTERFACE
An interface is a list of constants and method
headers. The methods are not implemented in the
interface.
A class that implements an interface must
implement each of the methods listed in the
interface.
Defining an Interface
the general form of an interface
access interface interface_name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value; // ...
return-type method-nameN(parameter-list); type
final-varnameN = value;
}
Example
interface MyInterface
{
//All the methods are public abstract by default
public void method1();
public void method2();
}
Implementing Interfaces
To implement an interface, include the implements
clause in a class definition, and then create the
methods defined by the interface.
The general form of a class that includes the
implements clause looks like this:
class classname [extends superclass] [implements
interface [,interface...]]
{
// class-body
}
Example
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1()
{ System.out.println("implementation of method1");
}
public void method2()
{ System.out.println("implementation of method2");
}
public static void main(String arg[])
{
XYZ obj = new XYZ();
obj. method1();
obj. method2();
}
}
Accessing Implementations Through Interface References
interface MyInterface
{
public void method1();
public void method2();
}
class XYZ implements MyInterface
{
public void method1()
{ System.out.println("implementation of method1");
}
public void method2()
{ System.out.println("implementation of method2");
}
public static void main(String arg[])
{
MyInterface obj = new XYZ();
obj. method1();
obj. method2();
}
}
Variables in Interface
interface doesn't allow you to declare any instance variables.
You can declare a constant variable, using static final.
By default these variables are public.
Example
interface MyInterface
{
int aConstant = 32; // a constant (public static final, by default)
double pi = 3.14159; // a constant (public static final, by default)
void methodA( int x ); // a method header (public, by default)
double methodB(); // a method header (public, by default)
}
interface SomeInterface
{
public final int x = 32;
public double y;//
public double addup( );
}
Inspect the interface. Is it correct?
Interfaces can be extended
One interface can inherit another by use of the keyword extends.
The syntax is the same as for inheriting classes.
Example
interface A
{
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A
{
void meth3();
}
When a class implements an interface that inherits another interface, it must provide implementations for all methods.
Example
interface A
{
void meth1();
void meth2();
}
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 InterfaceExtend
{
public static void main(String arg[])
{
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

working with interfaces in java programming

  • 1.
    INTERFACE An interface isa list of constants and method headers. The methods are not implemented in the interface. A class that implements an interface must implement each of the methods listed in the interface.
  • 2.
    Defining an Interface thegeneral form of an interface access interface interface_name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; }
  • 3.
    Example interface MyInterface { //All themethods are public abstract by default public void method1(); public void method2(); }
  • 4.
    Implementing Interfaces To implementan interface, include the implements clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the implements clause looks like this: class classname [extends superclass] [implements interface [,interface...]] { // class-body }
  • 5.
    Example interface MyInterface { public voidmethod1(); public void method2(); } class XYZ implements MyInterface { public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { XYZ obj = new XYZ(); obj. method1(); obj. method2(); } }
  • 6.
    Accessing Implementations ThroughInterface References interface MyInterface { public void method1(); public void method2(); } class XYZ implements MyInterface { public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { MyInterface obj = new XYZ(); obj. method1(); obj. method2(); } }
  • 7.
    Variables in Interface interfacedoesn't allow you to declare any instance variables. You can declare a constant variable, using static final. By default these variables are public. Example interface MyInterface { int aConstant = 32; // a constant (public static final, by default) double pi = 3.14159; // a constant (public static final, by default) void methodA( int x ); // a method header (public, by default) double methodB(); // a method header (public, by default) }
  • 8.
    interface SomeInterface { public finalint x = 32; public double y;// public double addup( ); } Inspect the interface. Is it correct?
  • 9.
    Interfaces can beextended One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. Example interface A { void meth1(); void meth2(); } // B now includes meth1() and meth2() -- it adds meth3(). interface B extends A { void meth3(); }
  • 10.
    When a classimplements an interface that inherits another interface, it must provide implementations for all methods. Example interface A { void meth1(); void meth2(); } 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()."); } }
  • 11.
    class InterfaceExtend { public staticvoid main(String arg[]) { MyClass ob = new MyClass(); ob.meth1(); ob.meth2(); ob.meth3(); } }