what is advantges of using interface reference instead of class refernce
-
-
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
You can implement the
ICheck
interface in any class/struct you like. You can then use the same method an any instances of any of those classes.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
You can only derive a class from a single other class. For example
public partial class frmMain : Form { ... }
But you can inherit as many Interfaces as you need. For example
public partial class frmMain : Form, ICloneable, IComparable, IDisposable { ... }
(Not that I am suggesting that is a sensible Interface list for a form, it's just an example)
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
In your example, there's very little advantage. Where the use of the interface comes into its own is when you want to perform a specific method, e.g. Dispose an object. Rather than having to use reflection to determine whether or not an object contains that method, you can use the interface to do the action. Consider the following snippet:
Data obj = new Data();
ICheck check = obj as ICheck;
if (check != null)
check.Fun();Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect. This really comes into its own if you are using something like Inversion of Control where you'd use the interface to retrieve the object, e.g.:
ICheck obj = MyIocContainer.Resolve(/* some reference to the Data class);
obj.Fun();I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Saturday, September 11, 2010 8:14 AM
-
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
Very little with a local variable -- very much with a parameter or property.
-
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
An important one - the
using ()
pattern wouldn't work without the use ofIDisposable
.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
In your example, there's very little advantage. Where the use of the interface comes into its own is when you want to perform a specific method, e.g. Dispose an object. Rather than having to use reflection to determine whether or not an object contains that method, you can use the interface to do the action. Consider the following snippet:
Data obj = new Data();
ICheck check = obj as ICheck;
if (check != null)
check.Fun();Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect. This really comes into its own if you are using something like Inversion of Control where you'd use the interface to retrieve the object, e.g.:
ICheck obj = MyIocContainer.Resolve(/* some reference to the Data class);
obj.Fun();I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Saturday, September 11, 2010 8:14 AM
Pete O'Hanlon wrote:
Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect.
Oh my god, no! If
Data
does not implementICheck
the code would not compile, at least in the statically typed polymorphic world! -
Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.
Go and google for Dependency inversion. Basically, you use interfaces to loosen dependencies between components. Consider:
public class Data : IData {
private IDataChecker mDataChecker;//...
public Data(IDataChecker pDataChecker) {
mDataChecker = pDataChecker;
}//...
}Your concrete
Data
depends only onIDataChecker
interface, not on its implementation. The consequence is that you can use many different implementations ofIDataChecker
without changingData
. -
Pete O'Hanlon wrote:
Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect.
Oh my god, no! If
Data
does not implementICheck
the code would not compile, at least in the statically typed polymorphic world!I checked the code I typed in and realised I'd forgotten to put
as ICheck
on the relevant line. I've amended it, and the code works exactly as I described. Thanks for bringing this to my attention.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads