You are on page 1of 17

Interface

• Is a blueprint of a class.
• Represents ISA relationship.
• Specifies what a class must do and not how(only signature &
no implementation of method).
• Collection of abstract methods.
• An interface contains only abstract methods & variable of the
type final, static.
• (methods in interface are abstract by default).
• All members in interface are public in nature by default.
 Interface contains
 All the abstract methods(no body)
 All variable to be public, static, final by default.
 implements keyword is used to inherit interface in interface / class.
 Interface cannot be instantiated(can’t create object of Inteface)
 An interface does not contain constructor.
 Imp Note: As Interface contains only abstract methods it is compulsory to
override same method in derived class with public keyword because Interface
contains abstract methods which is public in nature by default so In case of
Method Overriding, Child class method must have larger or equal access
specifier as Parent class method.
• interface keyword is used to create an Interface.
• interface <interface name>
{
// public static final variables by default
// public abstract methods by default
}

Java compiler adds public and abstract keyword before the method &
adds public static final keyword before data member in interface.
Interface used
• To achieve Multiple Inheritance
• To achieve abstraction.
Multiple Interface syntax
Interface I1
{
I1 I2
}

Interface I2
{
}
Test

Class Test implements I1,I2


{
.....
.....
}
• Java 8 can support default and static concrete methods in
interface.
Interface InterfaceDemo
{
default void display(){....}
Public Static void print(){....}
}

• Java 9 can support private methods in interface.


interface I1
{
void display();
}

class Test implements I1


{
public void display()
{
System.out.println("Interface class..Test");
}
}

class InterfaceDemo
{
public static void main(String args[])
{
Test T=new Test();
T.display();
}
}
Output:
Interface class..Test

/* In case of Method Overriding, Child class method must have larger or equal access specifier as Parent class
method */
interface I1
{
void display();
}
class Test implements I1
{
public void display1()
{
System.out.println("Interface Method Demo.... ");
}
}

class InterfaceMethodDemo1
{
public static void main(String args[])
{
Test T=new Test();
T.display();
}
}

/*
C:\Program Files\Java\jdk1.8.0_221\bin\reshma>javac InterfaceMethodDemo1.java
InterfaceMethodDemo1.java:5: error: Test is not abstract and does not override abstract method display() in I1
class Test implements I1
^
InterfaceMethodDemo1.java:18: error: cannot find symbol
T.display();
^
symbol: method display()
location: variable T of type Test
2 errors

*/
interface I1
{
final int a=10;
void display();
}
class Test implements I1
{
public void display()
{
a=a+10; //final variable trying to modify it generates following error
System.out.println("a= " + a);
}
}
class InterfaceVarDemo
{
public static void main(String args[])
{
Test T=new Test();
T.display();
}
}
Output: Error
C:\Program Files\Java\jdk1.8.0_221\bin\reshma>javac InterfaceVarDemo.java
InterfaceVarDemo.java:10: error: cannot assign a value to final variable a
a=a+10;
^
1 error
interface I1
{
final int a=10;
void display();
}
class Test implements I1
{
public void display()
{
//a=a+10;
System.out.println("a= " + a);
}
}
class InterfaceVarDemo
{
public static void main(String args[])
{
Test T=new Test();
T.display();
}
}

C:\Program Files\Java\jdk1.8.0_221\bin\reshma>javac InterfaceVarDemo.java


C:\Program Files\Java\jdk1.8.0_221\bin\reshma>java InterfaceVarDemo
a= 10
interface I1
{
int a=10;
void display();
}
class Test implements I1
{
public void display()
{
a=a+10;
System.out.println("a= " + a);
}
}

class InterfaceVarDemo1
{
public static void main(String args[])
{
Test T=new Test();
T.display();
}
}

/* Output : error=> If I m not declaring interface variable as final Java compiler by default considers it as final & doesnot
allow to modify its value
C:\Program Files\Java\jdk1.8.0_221\bin\reshma>javac InterfaceVarDemo1.java
InterfaceVarDemo1.java:10: error: cannot assign a value to final variable a
a=a+10;
^
1 error
*/
interface I1
{
void display();
}
interface I2
{
void show();
}
class Test implements I1, I2
{
public void display()
{
System.out.println("Interface Demo1.... ");
}
public void show()
{
System.out.println("Interface Demo2.... ");
}
}
class InterfaceMultipleInheritance
{
public static void main(String args[])
{
Test T=new Test();
T.display();
T.show();
}
}
/*C:\Program Files\Java\jdk1.8.0_221\bin\reshma>java InterfaceMultipleInheritance
Interface Demo1....
Interface Demo2.... */
Class Interface

variables all Static, final

methods Concrete, abstract, static abstract

Constructor support Does not

Scope Default/private/public/ Public


protected
What to do & how to do What to do

extends for deriving a new class implements

Object can be created Cannot create object

structure class classname interface InterfaceName


{ {
} }
Abstract class Interface

methods Concrete & abstract abstract

multiple inheritance Doesnot support multiple Multiple inheritance


inheritance
constructor constructor No constructor

structure abstract class classname interface InterfaceName


{...} {...}
scope default public

Inherit extends implements

You might also like