2. Interfaces are just a definition of behavior. Java Interface
is not a class but it is defined in away similar to the class
definition. An Interface describes the functions and
behaviors of a specific object type but doesn't implement
them. When a class implements an interface it has to
implement all methods declared in that interface.
Definition
An interface in java is a blueprint of a class. It has static
constants and abstract methods. The interface in java is a
mechanism to achieve abstraction. There can be only
abstract methods in the java interface not method body. It is
used to achieve abstraction and multiple inheritance in Java.
4. interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
class TestInterface4 implements Showable
{
public void print() { System.out.println("Hello"); }
public void show() { System.out.println("Welcome"); }
public static void main(String args[])
{
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
Example - Interface extends interface
5. interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj = new A6();
obj.print();
}
}
Example - Class implements interface
6. interface A
{
int x=10;
}
interface B
{
int x=100;
}
class Hello implements A,B
{
public static void Main(String args[])
{
// reference to x is ambiguous both variables are
x
System.out.println(x);
System.out.println(A.x);
System.out.println(B.x);
}
Example –Accessing Interface Variable
7. 1. Interface functions should be public and abstract.
2. Interface fields should be public and final.
3. Use the Keyword interface to define an interface.
4. If you define a public interface with
name myInterface the java file should be named
as myInterface.java (Similar to public class definition
rules).
5. A class implementing an interface should use the
keyword implements.
6. No objects can be created from an interface.
7. Interfaces don't have constructors as they can't be
initiated
8. An Interface can extends one or more interfaces.
9. You can define a reference of type interface but you
should assign to it an object instance of class type which
Some KEYNOTEs about Interface
8. 1) Java interfaces are slower and more limited than other
ones.
2) Interface should be used multiple number of times else
there is hardly any use of having them
1. Process standardization
2. Implement Multiple Inheritance
3. It is used to achieve abstraction.
4. Security Implementation
Advantage of Java
Disadvantages