Interface in Java
 An interface is like a class. It has static constants and abstract methods.
 Interfaces are declared using the interface keyword,
 method signature
 constant declarations (variable declarations that are declared to be
both static and final).
 An interface never contains method implementations (ie function
"bodies").
access 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;
}
 Access – either public or not used.
 Not used – default is an answer.
 Methods declared have no bodies and end with a semicolon after the
parameter list(abstract methods). Each class that includes an interface
must implement all of the methods.
 Variables – declared implicitly final and static, meaning they cannot be
changed by the implementing class. They must also be initialized with
a constant value.
 All methods and variables are implicitly public if the interface, itself, is
declared as public.
Understanding relationship between classes and interfaces
 To implement an interface
– include the implements clause in a class definition
access class classname [extends superclass] [implements interface
[,interface...]]
{
constant declarations ;
abstract method declarations;
}
 The methods that implement an interface must be declared public.
However, an interface is different from a class in several ways, including:
 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can
appear in an interface must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.
 An interface can be declared as member of a class or another
interface – called nested interface.
 How can we define i/f inside a class and how can we access it.
class A
{
interface Message()
{
void msg();
}
}
Class test implements A.Message
{
Public void msg()
{
S.o.p(“Hello”);
}
Public static void main(String args[])
{
A.Message message=new test();
message.msg();
}
}
 Interfaces Can Be Extended:
 One interface can inherit another interface - extend.
 The syntax is the same as for inheriting classes.
// One interface an extend another.
interface A
{
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A
{
void meth3();
}
interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
class A implements Showable
{
public void print()
{System.out.println("Hello");}
public void show()
{System.out.println("Welcome");}
public static void main(String args[])
{
A obj=new A();
obj.print();
obj.show();
}}

Javainterface

  • 1.
  • 2.
     An interfaceis like a class. It has static constants and abstract methods.  Interfaces are declared using the interface keyword,  method signature  constant declarations (variable declarations that are declared to be both static and final).  An interface never contains method implementations (ie function "bodies").
  • 3.
    access interface name { return-typemethod-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; }
  • 4.
     Access –either public or not used.  Not used – default is an answer.  Methods declared have no bodies and end with a semicolon after the parameter list(abstract methods). Each class that includes an interface must implement all of the methods.  Variables – declared implicitly final and static, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value.  All methods and variables are implicitly public if the interface, itself, is declared as public.
  • 5.
    Understanding relationship betweenclasses and interfaces
  • 7.
     To implementan interface – include the implements clause in a class definition access class classname [extends superclass] [implements interface [,interface...]] { constant declarations ; abstract method declarations; }  The methods that implement an interface must be declared public.
  • 8.
    However, an interfaceis different from a class in several ways, including:  You cannot instantiate an interface.  An interface does not contain any constructors.  All of the methods in an interface are abstract.  An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.  An interface is not extended by a class; it is implemented by a class.  An interface can extend multiple interfaces.
  • 9.
     An interfacecan be declared as member of a class or another interface – called nested interface.  How can we define i/f inside a class and how can we access it. class A { interface Message() { void msg(); } } Class test implements A.Message { Public void msg() { S.o.p(“Hello”); } Public static void main(String args[]) { A.Message message=new test(); message.msg(); } }
  • 10.
     Interfaces CanBe Extended:  One interface can inherit another interface - extend.  The syntax is the same as for inheriting classes. // One interface an extend another. interface A { void meth1(); void meth2(); } // B now includes meth1() and meth2() -- it adds meth3(). interface B extends A { void meth3(); }
  • 11.
    interface Printable { void print(); } interfaceShowable extends Printable { void show(); } class A implements Showable { public void print() {System.out.println("Hello");} public void show() {System.out.println("Welcome");} public static void main(String args[]) { A obj=new A(); obj.print(); obj.show(); }}