Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

INTERFACE

• Universal user defined data type.


• Supports multiple inheritance.
• Purely less execution time.
• To develop interface concept, use
keyword as interface.
• Mandinary to required Derived class
(execute program).
• Not possible to create object directly.
• possible to create object indirectly.
syntax:
interface <interface name>
{
- variable declaration with initilization.
- method declaration.(un defined method)
}
eg:
interface Demo
{
String file="balaji";
void check();
}
Definition for Interface?

• Collect of data member and un-defined methods.


• Data Member
– data type, variable name, value.
– By default, public static final.

syntax:
<data type> <variable name>=<value>;
eg:
int data=10;
String name="Balaji";
• Un-Defined methods
– Method declaration only.(called as
abstract method).
– By default, all method as public abstract.

syntax:
<return type> <method name>(<list of formal parameter>);
eg:
int details();
void file(String fileName);
Example for Interface
eg: Printable.java
interface Printable
{
int MIN=5;
void print();
}

Printable.class
interface Printable
{
public static final int MIN=5;
public abstract void print();
}
variable level in interface
public static final <data type> <variable name>=<value>;
(compile time JVM will add)
Here,
final - value Reusable but value not modified.
static - Memory space create only once.
public - every programmer to access.

Method level in interface


public abstract <return type> <method name>;
(compile time JVM will add)
Here
abstract - method declared.
public - access and reuse by other programmer.
Relationship between class and interface

• Class gets properties and field of interface.


• class inherits interface with help of
implements keyword.
eg:
class student implements Admin
{
}
• Supports IS-A relationship
Inheritance Relationship between class and interface
Example for interface
//base interface
interface Admin
{
int data=20; //main class
public class Interface_list
void list(int data);
{
}
public static void main(String[] arg)
{
//Derived class //Admin a=new Admin();//invalid
class Student implements Admin Admin a=new Student();
{ a.list(10);
public void list(int val) System.out.println(a.data);
{ }
System.out.println(val); }
OUTPUT
}
10
}
20
Example for interface (multiple inheritance)
//base interface
interface AdminHead
//main class
{
public class Interface_list
int data=20;
{
void list(int data);
public static void main(String[] arg)
}
{
interface Office
//AdminHead a=new
{ AdminHead();//invalid
String name="balaji"; AdminHead a=new Student();
String nameList(); a.list(10);
} Office o=new Student();
String name=o.nameList();
//Derived class System.out.println(a.data+" "+name);
class Student implements AdminHead,Office }
{ }
public void list(int val) OUTPUT
{ 10
System.out.println(val); 20 kumar
}
public String nameList()
{
return "kumar";
}
}
Example for interface (inheritance)
//base interface
interface Head
//main class
{
public class Interface_list
void food();
{
}
public static void main(String[] arg)
//intermediate
{
interface Tile extends Head
//Head a=new Head();//invalid
{
//Head a=new Tile();//invalid
String name="balaji";
Head o=new Custom();
String list();
o.food();
}
Tile t=new Custom();
String name=t.list();
//Derived class
System.out.println(name);
class Custom implements Tile
}
{
}
public void food()
{
System.out.println("Food");
}
OUTPUT
public String list()
Food
{
Dosai
return "Dosai";
}
}

You might also like