Java is the future !: Difference between Class reference and Interface reference

Sunday, April 24, 2011

Difference between Class reference and Interface reference

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:



Here if you want to do some generic work which is common for both the doctors (doCheckUp) you should use the interface like


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.


No comments:

Post a Comment