Chameli Devi School Of Engineering, Indore 
FTP 
ON 
Interface 
Presented By 
Shantilal Bhayal
Content 
 What is Interface. 
 Why use Interface. 
 How Define interface. 
 How use Interface. 
 Key Points for Interface. 
 Abstract Class Vs Interface. 
 Benefit Of Interface.
What is an Interface 
 Interface looks like class but it is not a class. 
 It is a Collection of : 
1. Public and Abstract method. 
2. Public ,static and final variables. 
 Cannot be instantiated. Only classes that implements interfaces can be 
instantiated. 
 Protocol for classes that completely separates specification/ behaviour 
from implementation.
Why use Interface 
1. To model multiple inheritance . 
2. To have unrelated classes implements similar methods(behavior). 
Example: 
Class Line and class MyInteger 
Interface: 
checkIsGreater(Object x, Object y) 
checkIsLess(Object x, Object y) 
checkIsEqual(Object x, Object y) 
3. To reveal an object's programming interface (functionality of the 
object) without revealing its implementation
How define Interface 
 An interface is defined much like a class but using interface keyword. 
 Syntax : 
access modifier interface Interface_name 
{ 
return-type method-name1(parameter-list); 
return-type method-name2(parameter-list); 
type varname1 = value; 
type varname2 = value; 
// ... return-type method-nameN(parameter-list); 
type final-varnameN = value; 
}
How use Interface 
 Once an interface has been defined, one or more classes can 
implement that interface. 
 Syntax: 
access_modifier class classname implements interface1, 
interface2….interfaceN 
{ 
// class-body 
} 
 Type signature of the implementing method must match exactly 
the type signature specified in the interface definition.
Key Points about Interface 
 We can’t instantiate an interface in java. 
 All the interface methods are by default abstract and public. 
 Variables declared in interface are public, static and final by 
default. 
 Interface variables must be initialized at the time of declaration 
otherwise compiler will through an error. 
 Any interface can extend any other interface but cannot 
implement it.
Key Point Continued… 
 Variable names conflicts can be resolved by interface name 
interface A 
{ 
int x=10; 
} 
interface B 
{ 
int x=100; 
} 
class Hello implement A,B 
{ 
public static void Main(String arg[]) 
{ 
System.out.println(x); // reference to x is ambiguous both variables are x 
System.out.println(A.x); 
System.out.println(B.x); 
} 
}
Continue.. 
 A class can implements any number of interfaces. 
 Interface cannot be declared as private and protected . 
 Class implementing any interface must implement all the 
methods, otherwise the class should be declared as “abstract”. 
 While providing implementation in class of any method of an 
interface, it needs to be mentioned as public. 
 Inside any implementation class, you cannot change the 
variables declared in interface.
Interface Abstract Class 
1 All methods in interface are by 
default Abstract. 
Abstract class can have Abstract as 
well as concrete Methods. 
2 Variables declared in interface is 
by default final 
An abstract class may contain non-final 
variables 
3 Members of a Java interface are 
public by default 
A Java abstract class can have the 
usual flavors of class members like 
private, protected, etc 
4 Interface do not have 
Constructors 
Abstract class can have 
Constructors 
5 Multiple inheritance possible Multiple inheritance not 
possible
Benefits of Interface 
 Provide multiple inheritance. 
 Allow standard sets of methods to be used across the class 
hierarchy. 
 As interfaces are declared independently of classes, they are 
unaffected by changes to specific classes.
Drawback of Interfaces 
 Interfaces cannot grow. 
 Cannot add more methods to the existing interface. 
 One has to re-implement all the classes which implements 
the interface when it grows.
Thank You

Interface

  • 1.
    Chameli Devi SchoolOf Engineering, Indore FTP ON Interface Presented By Shantilal Bhayal
  • 2.
    Content  Whatis Interface.  Why use Interface.  How Define interface.  How use Interface.  Key Points for Interface.  Abstract Class Vs Interface.  Benefit Of Interface.
  • 3.
    What is anInterface  Interface looks like class but it is not a class.  It is a Collection of : 1. Public and Abstract method. 2. Public ,static and final variables.  Cannot be instantiated. Only classes that implements interfaces can be instantiated.  Protocol for classes that completely separates specification/ behaviour from implementation.
  • 4.
    Why use Interface 1. To model multiple inheritance . 2. To have unrelated classes implements similar methods(behavior). Example: Class Line and class MyInteger Interface: checkIsGreater(Object x, Object y) checkIsLess(Object x, Object y) checkIsEqual(Object x, Object y) 3. To reveal an object's programming interface (functionality of the object) without revealing its implementation
  • 5.
    How define Interface  An interface is defined much like a class but using interface keyword.  Syntax : access modifier interface Interface_name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type varname1 = value; type varname2 = value; // ... return-type method-nameN(parameter-list); type final-varnameN = value; }
  • 6.
    How use Interface  Once an interface has been defined, one or more classes can implement that interface.  Syntax: access_modifier class classname implements interface1, interface2….interfaceN { // class-body }  Type signature of the implementing method must match exactly the type signature specified in the interface definition.
  • 7.
    Key Points aboutInterface  We can’t instantiate an interface in java.  All the interface methods are by default abstract and public.  Variables declared in interface are public, static and final by default.  Interface variables must be initialized at the time of declaration otherwise compiler will through an error.  Any interface can extend any other interface but cannot implement it.
  • 8.
    Key Point Continued…  Variable names conflicts can be resolved by interface name interface A { int x=10; } interface B { int x=100; } class Hello implement A,B { public static void Main(String arg[]) { System.out.println(x); // reference to x is ambiguous both variables are x System.out.println(A.x); System.out.println(B.x); } }
  • 9.
    Continue..  Aclass can implements any number of interfaces.  Interface cannot be declared as private and protected .  Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.  While providing implementation in class of any method of an interface, it needs to be mentioned as public.  Inside any implementation class, you cannot change the variables declared in interface.
  • 10.
    Interface Abstract Class 1 All methods in interface are by default Abstract. Abstract class can have Abstract as well as concrete Methods. 2 Variables declared in interface is by default final An abstract class may contain non-final variables 3 Members of a Java interface are public by default A Java abstract class can have the usual flavors of class members like private, protected, etc 4 Interface do not have Constructors Abstract class can have Constructors 5 Multiple inheritance possible Multiple inheritance not possible
  • 11.
    Benefits of Interface  Provide multiple inheritance.  Allow standard sets of methods to be used across the class hierarchy.  As interfaces are declared independently of classes, they are unaffected by changes to specific classes.
  • 12.
    Drawback of Interfaces  Interfaces cannot grow.  Cannot add more methods to the existing interface.  One has to re-implement all the classes which implements the interface when it grows.
  • 13.