Professional Documents
Culture Documents
• 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 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();
}
}
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