Interfaces PDF
Interfaces PDF
Interfaces PDF
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
Interfaces
1. Interface is also one of the type of class it contains only abstract methods. And Interfaces not
alternative for abstract class it is extension for abstract classes.
2. The abstract class contains atleast one abstract method but the interface contains only abstract
methods.
3. For the interfaces the compiler will generates .class files.
4. Interfaces giving the information about the functionalities and these are not giving the
information about internal implementation.
5. Inside the source file it is possible to declare any number of interfaces. And we are declaring the
interfaces by using interface keyword.
Syntax:-Interface interface-name
interface it1 { }
andbydefault above three methods are public
the interface contains constants and these constants by default public static final
Note-1 :- if u dont no the anything about implementation just we have the requirment specification
them we should go for inteface
Note-2:- If u know the implementation but not completly then we shold go for abstract class
Note-3 :-if you know the implementation completly then we should go for concreate class
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
Note: - If we are declaring or not each and every interface method by default public abstract. And the
interfaces are by default abstract hence for the interfaces object creation is not possible.
Example-1 :-
Interface constrains abstract methods and by default these methods are “public abstract “.
Interface contains abstract method for these abstract methods provide the implementation in
the implementation classes.
Implementation class is nothing but the class that implements particular interface.
While providing implementation of interface methods that implementation methods must be
public methods otherwise compiler generate error message “attempting to assign weaker
access privileges”.
Example-2:-
Interface contains abstract method for these abstract methods provide the implementation in
the implementation class.
If the implementation class is unable to provide the implementation of all abstract methods
then declare implementation class with abstract modifier, take child class of implementation
class then complete the implementation of remaining abstract methods.
In java it is possible to take any number of child classes but at final complete the
implementation of all abstract methods.
//Test2is abstract class contains 1 abstract methodm3()hence object creation not possible
abstract class Test2 extends Test1
{ public void m2()
{ System.out.println("m2 method"); }
};
//Test3 is normal class because it contains only normal methods hence object creation possible
class Test3 extends Test2
{ public void m3()
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
{ System.out.println("m3 method"); }
public static void main(String[] args)
{ Test3 t = new Test3();
t.m1(); t.m2(); t.m3();
}
};
interface It1 //abstract
{ void m1(); //public abstract
void m2();
void m3();
}
abstract class Test implements It1
{ public void m1(){System.out.println("m1 method");}
};
abstract class Test1 extends Test
{ public void m2(){System.out.println("m2 method");}
};
class Test2 extends Test1
{ public void m3(){System.out.println("m3 method");}
public static void main(String[] args)
{ Test2 t = new Test2();
t.m1();
t.m2();
t.m3();
}
};
Example 3:-
The interface reference variables is able to hold child class objects.
interface It1 // interface declaration
{ void m1(); //abstract method by default [public abstract]
void m2(); //abstract method by default [public abstract]
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
interface It1
{ void m1();
}
interface It2
{ void m2();
}
interface It3 extends It1,It2
{ void m3();
}
class Test implements It1
{ 1 method
};
class Test implements It1,It2
{ 2 methods
};
class Test implements It1,It2,It3
{ 3 methods
};
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
Example-1:-
Inside the interfaces it is possible to declare variables and methods.
By default interface methods are public abstract and by default interface variables are public
static final.
The final variables are replaced with their values at compilation time only.
t.m1();
}
};
Application after compilation:-(.class file)
interface It1
{ public abstract void m1();// compiler generate public abstract
public static final int a = 10;//public static final generated by compiler
}
class Test implements It1
{ public void m1()
{ System.out.println("m1 method");
System.out.println(10);//a is final variable hence it replaced at compilation time only
}
public static void main(String[] args)
{ Test t = new Test();
t.m1();
}
};
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 9|Page
JAVA Means DURGASOFT
Message.java:-
packagecom.sravya.declarations;
public interface Message
{ void morn();
void even();
voidgn();
}
Helper.java-
packagecom.sravya.helper;
importcom.sravya.declarations.Message;
public abstract class Helper implements Message
{ public void gn(){System.out.println("good night from helper class");}
}
TestClient1.java:-
packagecom.sravya.client;
importcom.sravya.declarations.Message;
class TestClient1 implements Message
{ public void morn(){System.out.println("good morning");}
public void even(){System.out.println("good evening");}
public void gn(){System.out.println("good 9t");}
public static void main(String[] args)
{ TestClient1 t = new TestClient1();
t.morn();
t.even();
t.gn();
}
}
TestClient2.java:-
packagecom.sravya.client;
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 10 | P a g e
JAVA Means DURGASOFT
importcom.sravya.helper.Helper;
class TestClient2 extends Helper
{ public void morn(){System.out.println("good morning");}
public void even(){System.out.println("good evening");}
public static void main(String[] args)
{ TestClient2 t = new TestClient2();
t.morn();
t.even();
t.gn();
}
}
D:\>javac -d . Message.java
D:\>javac -d . Helper.java
D:\>javac -d . TestClient1.java
D:\>javac -d . TestClient2.java
D:\>java com.sravya.client.TestClient1
good morning
good evening
good 9t
D:\>java com.sravya.client.TestClient2
good morning
good evening
good night from helper class
BusinessLogic.java:-
packagecom.dss.businesslogics;
importcom.dss.declarations.Message;
public class BusinessLogic implements Message
{ public void msg1(){System.out.println("i like you");}
public void msg2(){System.out.println("i miss you");}
}
TestClient.java:-
packagecom.dss.client;
importcom.dss.businesslogics.BusinessLogic;
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 11 | P a g e
JAVA Means DURGASOFT
class TestClient
{ public static void main(String[] args)
{ BusinessLogic b = new BusinessLogic();
b.msg1();
b.msg2();
Message b1 = new BusinessLogic();
b1.msg1();
b1.msg2();
}
}
};
Case 2:
class Test implements it2
{ provide the implementation of 4 methods m1() & m2() & m3() & m4()
};
Case 3:-
class Test implements it3
{ provide the implementation of 6 methods m1() & m2() & m3() & m4() & m5() & m6()
};
Case 4:-
class Test implements it4
{ provide the implementation of 7 methodsm1() & m2() & m3() & m4() & m5() & m6() & m7()
};
Case 6:-
class Test implements it1,it2 //one class is able to implements multiple interfaces
{ provide the implementation of 4 methods m1() & m2() & m3() & m4()
};
Case 7:-
class Test implements it1,it3
{ provide the implementation of 6 methods m1() & m2() & m3() & m4() & m5() & m6
};
Case 8:-
class Test implements it2,it3
{ provide the implementation of 6 methods m1() & m2() & m3() & m4() & m5() & m6
};
Case 9:-
class Test implements it2,it4
{ provide the implementation of 7 methodsm1() & m2() & m3() & m4() & m5() & m6 & m7()
};
Case 10:-
class Test implements it1,it2,it3
{ provide the implementation of 6 methods m1() & m2() & m3() & m4() & m5() & m6
};
Case 11:-
class Test implements it1,it2,it3,it4
{ provide the implementation of 7 methodsm1() & m2() & m3() & m4() & m5() & m6 & m7()
};
Nested interfaces:-
Example :- declaring interface inside the class is called nested interface.
class A
{ interface it1//nested interface declared in A class
{ void add(); }
};
class Test implements A.it1
{ public void add()
{ System.out.println("add method");
}
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 13 | P a g e
JAVA Means DURGASOFT
Adaptor class:-
It is aintermediate class between the interface and user defined class. And it contains empty
implementation of interface methods.
Example:-
interface It // interface
{ void m1():
void m2();
;;;;;;;;
void m100();
}
class X implements It //adaptor class
{ public void m1(){}
public void m2(){}
;;;;;;;;
public void m100{}
};
//user defined class implementing interface
class Test implements It
{ must provide 100 methods impl
};
//user defined class extending Adaptor class(X)
class Test extends X
{ override required methods because already X contins empty implementions
};
void night();
}
HelperAdaptor.java:- Adaptor class
packagecom.dss.helper;
importcom.dss.declarations.Message;
public class HelperAdaptor implements Message
{ public void night(){}
public void eve(){}
public void morn(){}
}
GoodStudent.java:-
packagecom.dss.bl;
importcom.dss.declarations.Message;
public class GoodStudent implements Message
{ public void morn(){System.out.println("good morning ratan");}
public void eve(){System.out.println("good evening ratan");}
public void night(){System.out.println("good nightratan");}
}
TestClient.java:-
packagecom.dss.client;
importcom.dss.bl.GoodStudent;
importcom.dss.bl.Student;
classTestClient
{ public static void main(String[] args)
{ GoodStudent s = new GoodStudent();
s.eve();s.morn();s.night();
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 17 | P a g e
JAVA Means DURGASOFT
com
|-->dss
|--->declarations
| |-->Message.class
|-->helper
| |--->HelperAdaptor.class
|-->bl
| |-->GoodStudent.class
| |-->Student.class
|-->client
|-->TestClient.class
Example :-
Demo.java
package a;
public interface Demo
{ public void sayHello(String msg);
}
ImplClass:-
package a;
class Test implements Demo
{ public void sayHello(String msg)//overriding method of Demo interface
{ System.out.println("hi ratan--->"+msg);
}
};
public class ImplClass
{ public Test objectcreation()//it returns Test class Object
{ Test t = new Test();
return t;
}
}
Client.java
importa.ImplClass;
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 18 | P a g e
JAVA Means DURGASOFT
importa.Demo;
class Client
{ public static void main(String[] args)
{ ImplClass i = new ImplClass();
Demo d = i.objectcreation();
//it returns Object of class Test but we don’t know internally which object is created
d.sayHello("hello");
}
}
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 19 | P a g e
JAVA Means DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 20 | P a g e