My code is:
class Sample implements interf {
public void intmethod(){ //some code.... }
}
public interface interf{ public void intmethod() }
My question is what is difference between the following two statements
Sample sam = new Sample();
interf int = new Sample();
Let's say you have:
class Sample implements interf {
public void intmethod(){ //some code.... }
public void sampleMethod() { // code only relevant to Sample objects}
}
class Sample2 implements interf {
public void intmethod(){ //some code.... }
public void sample2Method() { // code only relevant to Sample2 objects }
}
public interface interf{ public void intmethod() }
You will be able to do:
Sample sam = new Sample();
interf intObj = new Sample();
Sample2 sam2 = new Sample2();
sam.intmethod();
sam.sampleMethod();
intObj.intmethod();
// the las one will give you an erro because sampleMethod is not defined for interf
//intObj.sampleMethod()
sam.intmethod();
sam2.sampleMethod2();
but defining interf objects will allow you to do this:
List<interf> myList = new ArrayList<interf>();
myList.add(sam);
myList.add(intObj);
myList.add(sam2);
for (obj : myList) {
obj.intmethod();
}
Oracle tutorial pages for interfaces and polymorphism:
http://download.oracle.com/javase/tutorial/java/concepts/interface.html
http://download.oracle.com/javase/tutorial/java/IandI/polymorphism.html
Wikipedia has some interesting examples in more than one language:
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
With Sample sam = new Sample();
, you have a reference of type Sample
and you can work with sam
in a way that the Sample
class defines.
With interf int = new Sample();
, although you have an object of type Sample
, you can only work it in a way that interf
defines (for example, methods that are available in Sample
but are not declared in interf
cannot be invoked without casting the reference to Sample
). But, you will be able to use this anywhere that an interf
is expected, irrespective of the underlying implementation.
The second form is usually preferred since you can swap out the underlying implementation at a later time without worrying about refactoring all of your code.
See more discussions on using interfaces vs. the implementing classes:
- Java - declaring from Interface type instead of Class
- Type List vs type ArrayList in Java
One reference will allow you to call methods declared in the class, the other allows you to call methods declared in the interface. In this very simple case, where the only methods in Sample
are those defined in interf
, it should not make any difference to you.
Sample sam = new Sample();
allows to access all the methods of class Sample.
interf int = new Sample();
allows to access only those methods that interface is having.
Interface provides the base for the concrete classes. It defines the contract which must be full filled by the implementing classes. So it can be use full in case where you need polymorphism. for example if you have some classes and interface like:
interface Doctor{ void doCheckUp(); }
class HumanDoctor implements doctor{
public void doCheckUp(){
//checkup code here }
public doHumanRelatedStuff(){ // Human Related Stuff }
}
class AnimalDoctor implements doctor{ public void doCheckUp(){ //checkup code here }
public doAnimalRelatedStuff(){ // Animal Related Stuff } }
Here if you want to do some generic work which is common for both the doctors (doCheckUp) you should use the interface like
public void routineChecup(Doctor d){
d.doCheckUp();
}
In this case no matter which object you are passing it will call the doCheckUp method of corresponding object.
But your concrete class can have some extra methods and if you want to use those methods then you have to call the methods using corresponding concrete class objects.
So If you want to use the generic common methods, you should use the interface instance.