AIM POINT INFOSYSTEM PVT. LTD.
2019
INTERFACE
Ambiguity Error in multiple inheritance in hybrid Inheritance.
B C
In above example the properties of class A inherit twice in class D -
i. Through Class B
ii. Through Class C
In this class if we are using the property of class A in class D, compiler will give Ambiguity
Error in the property of class A (those are inherited twice). and the size of class D will be
increased unnecessarily. Java ignore these kind of Ambiguities error in the implementation of
inheritance , that‟s why java does not support multiple inheritance and the Alternative Solution of
multiple Inheritance in an “interface”.
“interface”
It is a kind of class means it contains methods and attributes.
Interface contain only public / abstract method (define or not define ) and Attributes will
be public / static / final ( define or not define )
These asbstract method of interface must be defined by the class which is
implementing these interface.
One interface can be inherit (extends) by another interface but one interface cannot be
inherit into class.
interface is the solution of multiple Inheritance in Java.
One class can be implement more than one interface.
Interface is used to implement polymorphism in java.
If we are declaring public Interface then its method will be automatically public.
If we are using default access specifier (means no access Specifier) then this interface
belong to the Current package and can be implemented by any class of this Package.
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 1
AIM POINT INFOSYSTEM PVT. LTD. 2019
Syntax :-
<Access Specifier(default/public) > interface interface_name
Attributes (by default public / static / final )
Methods (by default public / abstract )
Example
i. public interface i1
{ int x=20 ; (by default public / static / final )
void show( ) ; (by default public / abstract )
}
ii. Interface i1
{ int x=20;
void show();
}
Implementing Interface
Syntax-
Interface <interface name>
{
--------------------------;
------------------------- ;
}
class class_name implements <interface_name>
{
------------------------------;
-------------------------------
interface & implements are keywords.
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 2
AIM POINT INFOSYSTEM PVT. LTD. 2019
Difference between class and Interface
Class interface
Class can contain final or non-final Interface contain only final
variable. Variable.
Methods of class can be abstract /Non- Methods of Interface must be
abstract. abstract.
Can be instantiate (means object may be Cannot be instantiate (means
created). object cannot be created).
The member of class can be private/ Members of Interface must be
protected / public. public.
Different Syntaxes between interface and class
i. Extending interface
interface i1
{ -------------------------;
-------------------------;
}
interface i2 extends i1
{ ------------------------;
------------------------;
}
ii. Implementing Interface
interface i1
{ ------------------------;
------------------------;
}
Class c1 implements i1
{ ----------------------;
----------------------;
}
iii. Multiple Inheritance
interface i1
{ ------------------------;
------------------------;
}
interface i2
{ ------------------------;
------------------------; }
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 3
AIM POINT INFOSYSTEM PVT. LTD. 2019
a. interface i3 extends i1 , i2
{ ------------------------;
------------------------;
}
//or//
b. Class c1 implements i1 , i2
{ ------------------------;
------------------------;
}
iv. Hybrid Inheritance
interface i1
{ ------------------------;
------------------------;
}
interface i2 extends i1 interface i3 extends i1
{ ------------------------; { ---------------------- ;
------------------------; ------------------------;
} }
Class c1 implements i2 , i3
{ ------------------------;
------------------------;
}
Difference between “extends” and “implements”
One class can extends only one class at a time.
One Interface can extends any number of interface at a time.
A class can implements any no of interface at a time.
A class can extends only one class and can implements any number of interfaces
simultaneously at a time.
I. X extends Y, Z
II. X extends Y implements Z
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 4
AIM POINT INFOSYSTEM PVT. LTD. 2019
III. X implements Y, Z
IV. X implements Y extends Z
//Write a program demonstrate the concepts of Interface
interface ArrayType
{ int noOfElements = 10 ;//by default final
void average( ) ;//by default abstract
}
class IntArray implements ArrayType
{ int x[ ] ; // array reference
public IntArray( )
{ x = new int[noOfElements] ; }
public void getData( )
{int i = 0 ;
//noOfElements=20;//interface variable cannot modify because by default it is final
for(int t : x )
x[i++]= i ;
}
public void average( )
{ int sum = 0 ;
for ( int t : x )
sum = sum + t ;
System . out .println ( " Average Of Int Array = " + ( sum / 10.0f) ) ;
}
}
class FloatArray implements ArrayType
{ float x[ ] ; // array reference
public FloatArray( )
{ x = new float[noOfElements] ; }
public void getData( )
{int i = 0 ;
for(float t : x )
x[i++]= i*2.1f;
}
public void average( )
{ float sum = 0 ;
for ( float t : x )
sum = sum + t ;
System . out .println ( " Average Of Float Array = " + ( sum / 10.0f) ) ;
}
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 5
AIM POINT INFOSYSTEM PVT. LTD. 2019
class AverageArray
{
public static void main( String a [ ] )
{
IntArray i1 = new IntArray( ) ;
FloatArray f1 = new FloatArray( ) ;
i1 . getData( ) ;
f1.getData( ) ;
i1.average( ) ;
f1.average( ) ;
}
}
Interface method
Interface methods must be „public‟ and „abstract‟ (or by default “this” method will be public and
Abstract ).
interface l1
{ void test( );
public void test( );
abstract void test( );
public abstract void test( );
Note : All above declarations are same
Conflict rules of interface
1) If two interface contain same methods (same prototype) then the class which is
implementing these interface should have only one definition.
Interface Left interface Right
{ { void test( );
void test( ); }
}
class Demo implements Left ,Right
{
Void test( )
{ ----------;
----------;
}
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 6
AIM POINT INFOSYSTEM PVT. LTD. 2019
2) If two interfaces contain same method with different prototype then the class which is
implementing these interfaces should have the definition of the both methods other wise
result will be error.
Interface Left interface Right
{ void test( ); { void test(int );
} }
Class Demo implements Left, Right
{ void test( )
{ ---------------------;
---------------------;
}
void test(int a)
{ ---------------------;
---------------------;
}
}
3) If two interfaces contain same method with same name (but with different return type)
then these interfaces method cannot be define in class (which is implementing it)
otherwise result will be error.
interface Left interface Right
{ {
Void test( ); int test( );
} }
class Demo implements Left , Right
{
//can‟t define interface methods, because these are not overloaded methods.
}
Interface Attributes
Interface Attributes must be public, static and final (or by default it will be public, static and final).
interface i1
{
int Count=10;
}
public int Count=10;
static int Count=10;
final int Count=10;
public static int Count=10;
static final int Count=10;
public final int Count=10;
public static final int Count=10;
Note : All above declarations are same
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 7
AIM POINT INFOSYSTEM PVT. LTD. 2019
NOTE:- Interface attributes cannot be modify in the class which is implementing it.
interface i1
int count=10;
class c1 implements i1
void abc( )
count=20;//error because attribute cannot be modified because it is final
Conflict Rule of Interface Attribute
Interface variable ,class variable and local variable can be declare with same name.
Interface i1
{ int count=10;
}
class Test implements i1
{ static int count=20;
Void abc()
{ int count= 30;
System.out.println (count);
System.out.println(Test.count);
System.out.println ( i1.count);
}
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 8
AIM POINT INFOSYSTEM PVT. LTD. 2019
Implementation of Hybrid Inheritance using Interface
interface College
intcolgCode=001
String colgName=”OIST”
interface Branch interface HOD
void getBranch( ) void getHod( )
void showBranch( ) void showHod( )
class Student
String sno, bcode,hodcode,
String sname,bname
void getStudent( )
void showStudent( )
import java.util.Scanner ;
interface ColgInter
{ intcolgCode=101 ;
String colgName = "OIST";
}
interface BranchInter extends ColgInter
{ void getBranch( ) ;
void showBranch( ) ;
}
interface HodInter extends ColgInter
{
void getHod( ) ;
void showHod( ) ;
}
class Student implements BranchInter , HodInter
{ String sno , bCode , hodCode ;
String sname , bName , hodName ;
Scanner o1 = new Scanner(System.in);
public void getBranch( )
{ System.out.print("\n Enter Branch Code & Branch Name ");
bCode=o1.next( ) ; bName=o1.next( ) ;
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 9
AIM POINT INFOSYSTEM PVT. LTD. 2019
public void showBranch( )
{
System.out.print("\n BRANCH CODE= "+
bCode+" BRANCH NAME = "+bName );
}
public void getHod( )
{
System.out.print("\n Enter HOD Code & HOD Name ");
hodCode=o1.next( ) ; hodName=o1.next( ) ;
}
public void showHod( )
{
System.out.print("\n HOD CODE= "+
hodCode+" HOD NAME = "+hodName );
}
void getStudent( )
{
System.out.print("\n Enter Student No & Student Name ");
sno=o1.next( ) ; sname=o1.next( ) ;
getBranch( ) ; getHod( ) ;
}
void showStudent ( )
{
System.out.print("\n ***** "+colgName+"("+colgCode+")"+"*****" );
System.out.print("\n STUDENT NO= "+
sno+" STUDENT NAME = "+sname );
showBranch( ) ; showHod( ) ;
}
}
class StudentRecord
{
public static void main( String a[ ] )
{
Student s1=new Student( ) ;
Student s2=new Student( ) ;
s1 . getStudent( ) ; s2 . getStudent( ) ;
s1 . showStudent( ) ; s2 . showStudent( ) ;
}
}
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 10
AIM POINT INFOSYSTEM PVT. LTD. 2019
Differences between “interface” and “abstract” class
“interface” “abstract” class
It is fully implemented by another class . “abstract” class may be fully / partially
Implemented by another class .
Methods of “interface” must be abstract Don‟t need .
And public .
“interface” variable must be public , Don‟t need .
static and final .
”interface” does not have constructor . It can have constructor .
“interface” variables must be initialized Don‟t need .
,otherwise error .
FACULTY : SANDEEP SAHU( M.Tech(CSE)) Page 11