2. INTERFACES
• An interface declares (describes) methods but does not supply bodies for them
• All the methods are implicitly public and abstract
• You can add these qualifiers if you like, but why bother?
• You cannot instantiate an interface
• An interface is like a very abstract class—none of its methods are defined
• An interface may also contain constants (final variables)
3. DESIGNING INTERFACES
• An interface is created with the following syntax
modifier interface interfaceID
{
//constants/method signatures
}
4. INTERFACES (CONT)
• An interface can extend other interfaces with the following syntax:
modifier interface interfaceID extends comma-delimited-list-of-
interfaces
{
//constants/method signatures
}
• Obviously, any class which implements a “sub-interface” will have to implement
each of the methods contained in it’s “super-interfaces”
5. IMPLEMENTING AN INTERFACE
• You extend a class, but you implement an interface
• A class can only extend (subclass) one other class, but it can implement as many
interfaces as you like
• Example:
class MyListener
implements KeyListener, ActionListener { … }
• When you say a class implements an interface, you are promising to define all the
methods that were declared in the interface
6. PARTIALLY IMPLEMENTING AN INTERFACE
• It is possible to define some but not all of the methods defined in an interface:
abstract class MyKeyListener implements KeyListener
{
public void keyTyped(KeyEvent e) {...};
}
• Since this class does not supply all the methods it has promised, it is an abstract
class
• You must label it as such with the keyword abstract
• You can even extend an interface (to add methods):
• interface FunkyKeyListener extends KeyListener { ... }
7. WHAT ARE INTERFACES FOR?
• Reason 1: A class can only extend one other class, but it can implement multiple
interfaces
• This lets the class fill multiple “roles”
• In writing Applets, it is common to have one class implement several different listeners
• Example:
class MyApplet extends Applet
implements ActionListener, KeyListener {
...
}
• Reason 2: You can write methods that work for more than one kind of class
8. IMPLEMENTING INTERFACES
• interface area //interface defined
{
final static float pi=3.14F;
float compute(float x, float y);
}
class rect implements area // interface implemented
{
public float compute(float x, float y)
{
return(x * y);
}
}
9. IMPLEMENTING MULTIPLE INHERITANCE
import java.io.*;
class student
{
String name="SACHIN";
String dept="MCA";
int rollno;
void getnumber(int n)
{
rollno=n;
}
void display()
{
System.out.println("STUDENT DATABASE USING MULTIPLE INHERITANCE AND INTERFACE CONCEPTS");
System.out.println("Student Name :"+name);
System.out.println("Rollno :"+rollno);
System.out.println("Department :"+dept);
}
}
10. class test extends student
{
int m1,m2,m3;
void getmarks(int a,int b,int c)
{ m1=a;
m2=b;
m3=c;
}
void displaymarks()
{ System.out.println("Marks Obtained");
System.out.println("Subject1 :="+m1);
System.out.println("Subject2 :="+m2);
System.out.println("Subject3 :="+m3);
} }
interface sports
{ float sportmarks=7.0F;
void dispsport();
}
11. class results extends test implements sports
{
float total,avg;
public void dispsport()
{
System.out.println("Sport Marks :="+sportmarks);
}
void displaydetails()
{
total=m1+m2+m3;
avg=total/3;
display();
displaymarks();
dispsport();
System.out.println("Total marks Secured:="+total);
System.out.println("Average :="+avg);
}
12. class multiple
{
public static void main(String a[])
{
results r1=new results();
r1.getnumber(4006);
r1.getmarks(80,90,95);
r1.displaydetails();
}
}
13. JAVA NESTED INTERFACE
• An interface i.e. declared within another interface or class is known as nested
interface.
• The nested interfaces are used to group related interfaces so that they can be easy to
maintain.
• The nested interface must be referred by the outer interface or class. It can't be
accessed directly.
• Nested interface must be public if it is declared inside the interface but it can have
any access modifier if declared within the class.
• Nested interfaces are declared static implicitely.
14. JAVA NESTED INTERFACE
• Syntax of nested interface which is declared within the interface
• interface interface_name
{
...
interface nested_interface_name
{
...
}
}
15. JAVA NESTED INTERFACE
• Syntax of nested interface which is declared within the class
• class class_name{
...
interface nested_interface_name{
...
}
}
16. EXAMPLE
interface Showable
{
void show();
interface Message {
void msg();
}
}
class Test implements Showable.Message
{
public void msg()
{System.out.println("Hello nested interface");}
public static void main(String args[])
{
Showable.Message message=new Test(); //upcasting here
message.msg();
}
}