Professional Documents
Culture Documents
2019
INTERFACE
Ambiguity Error in multiple inheritance in hybrid Inheritance.
B C
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”
Syntax :-
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 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.
i. Extending interface
interface i1
{ -------------------------;
-------------------------;
}
interface i2 extends i1
{ ------------------------;
------------------------;
}
a. interface i3 extends i1 , i2
{ ------------------------;
------------------------;
}
//or//
b. Class c1 implements i1 , i2
{ ------------------------;
------------------------;
}
{ ------------------------; { ---------------------- ;
------------------------; ------------------------;
} }
Class c1 implements i2 , i3
{ ------------------------;
------------------------;
}
I. X extends Y, Z
III. X implements Y, Z
interface ArrayType
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( );
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( )
{ ----------;
----------;
}
}
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:- Interface attributes cannot be modify in the class which is implementing it.
interface i1
int count=10;
class c1 implements i1
void abc( )
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);
}
}
interface College
intcolgCode=001
String colgName=”OIST”
class Student
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