| Author: pallavi 09 Apr 2008 | Member Level: Gold | Rating: Points: 2 |
If you have been sick, you would have had medicines for sure. One of the tablets that you would have received is a capsule. I hope you would have seen it.
Now this kind of tablet, is a bit different from the others. From the outside its just a cap, and it hides everything that is contained within. But what lies inside may be 2 or 3 or more powders loosely arranged and packed within.
An object is something similar. It is created with the immense power of a class. while the composition of the class could be anything (as compared to the capsule), one may not know wht is contained when you create the object handle you may say object here is like the capsule to all those who want to consume it in their programs.
So, with this object one can use its inherent power.
Thus this concept of hiding away its true power is known as Encapsulation.
Hope this helps. also visit
http://www.dotnetspider.com/qa/Question119807.aspx
|
| Author: pallavi 09 Apr 2008 | Member Level: Gold | Rating: Points: 2 |
We use Abstract class and interface to enforce some rules to the classes which extends/implements. For example we can define a class say "Bird" and we can declare methods say "Fly()", "Walk()". This means whatever the class that is derived from Bird has to override or give definition for Fly() and Walk() and therefore we are making sure that all the derived classes follows or has the common functionalities. In other way the classes derived from superclass should have common properties. In addition to this the derive class can have its own methods like "Swim()"...
In case of Abstract class we can define COMMON functionalities in super class and those can be used in the derived class where as in Interface we cant do that. ( this i would say as advantage of abstract class)
In case of Interface the derived class can implement any number of interface but restricted to extend only one abstract class (this i would say as advantage of Interface)
|
| Author: pallavi 09 Apr 2008 | Member Level: Gold | Rating: Points: 2 |
An interface is like a contract which means that you are telling that say if you want to talk to me, you need to use English or German etc..etc. In this case a class.
So what am I talking about, an Interface allows you to define that if your class wants to follow an interface, it needs to apply those methods. However its up to the user, the way they want to apply this implementation.
Now suppose you created an interface called ITest like so
Interface Itest
{
void myTest() // Please note only the declaration of function is there, not implementation
void yourTest() // Same here
}
Now if user A, and B decide to implement this interface, they can do that but they must provide implementation of these methods in order for them to use it.
User A does it like so
CLASS A : Itest
{
void mytest()
{
console.writeline("A's mytest");
}
void yourtest()
{
console.writeline("A's yourtest");
}
}
B's implementation CLASS B : Itest
{
void mytest()
{
console.writeline("B's mytest");
}
void yourtest()
{
console.writeline("B's yourtest");
}
}
So you see they both can both sign the contract to implement ITest but can have different implementations. So what are the benefits.
1. It allows the code designers flexibility so that they can follow an interface but still code they want.
2. It allows the code to work without actual implementation
3. It allows for multiple inheritance. Meaning you can have multiple interfaces.
|