I am trying to understand the advantages of using interfaces. I found this example on the internet:
Class Person Implements ICloneable
Function Clone() As Object Implements ICloneable.Clone Return Me.MemberwiseClone End Function
End Class
which creates a shallow copy of an object. I tried it out and it works fine. Now it tried the same thing without the "implements icloneable" command and withouth the "implements icloneable.clone" definition for the method. It works in the same exact way.
Why, then, use an interface? The only advantage I see is forcing object implementation to use a specific number and type of interfaces but that doesnt really help me with code reuse... what am I missing?
"Never argue with an idiot; He will drag you down to his level and beat you with experience"
But.... I could do the same thing without the interface..... If I am sure that both classes implement the Start() and MoveBackward() methods I can call them directly without using any interface......
I still cannot see why or how can I benefit from implementing a common interface.....
"Never argue with an idiot; He will drag you down to his level and beat you with experience"
The only way you can be absolutely sure that something implements the start/movebackwards/etc methods is by implementing the interface. Sure you can look at every class and 'see' that they do, but you cannot (programatically) be sure of it.
As such, the above example would be impossible to do without the interface, since you would have to type cast each object to its derived class:
for (int i = 0; i < obj.Count; i++)
{
if(o is Car) {
((Car)obj[i]).Start();
((Car)obj[i]).MoveBackward();
} else if (o is Bike) {
((Bike)obj[i]).Start();
((Bike)obj[i]).MoveBackward();
}
}
As you can imagine, that would become unmaintainable -very fast- if you planned on implementing more than a few types. So essentially, the interface lets you programatically know that the object contains certain methods, so that you can use those methods without
having to know what the derived class actually is.
Those are pretty much their main advantages:
-Knowing for certain that specific methods and/or properties are available
-Ablility to call those methods/properties without knowing anything about the derived class
It doesnt seem like much, but when developing systems that require highly reusable/modular code, or when dealing with abstractions where information about the derived classes is simply not available (such as remoting), interfaces are essential.
javiguillen
Contributor
2432 Points
526 Posts
Advantages of using Interfaces
May 24, 2006 05:46 PM|LINK
I am trying to understand the advantages of using interfaces. I found this example on the internet:
Class Person
Implements ICloneable
Function Clone() As Object Implements ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
which creates a shallow copy of an object. I tried it out and it works fine. Now it tried the same thing without the "implements icloneable" command and withouth the "implements icloneable.clone" definition for the method. It works in the same exact way.
Why, then, use an interface? The only advantage I see is forcing object implementation to use a specific number and type of interfaces but that doesnt really help me with code reuse... what am I missing?
martinbl
Participant
1700 Points
355 Posts
Re: Advantages of using Interfaces
May 24, 2006 06:07 PM|LINK
using interface make it possible to treat different type of object the same way.
you could define an Interface IDrivable could define method such as StartEngine, MoveFoward, MoveBackward, TurnLeft and TurnRight.
you could then have a Car class which implement the IDrivable interface as well as Bike Class.
and in your code, you could have a loop to mixed array of Car/Bike and
obj[0] = new Car();
obj[1] = new Car();
obj[2] = new Bike();
and have a
for (int i = 0; i < obj.Count; i++)
{
((IDrivable)obj[i]).Start();
((IDrivable)obj[i]).MoveBackward();
}
This is only a simple application of what an Interface can do !
Hope this help!
javiguillen
Contributor
2432 Points
526 Posts
Re: Advantages of using Interfaces
May 24, 2006 06:30 PM|LINK
But.... I could do the same thing without the interface..... If I am sure that both classes implement the Start() and MoveBackward() methods I can call them directly without using any interface......
I still cannot see why or how can I benefit from implementing a common interface.....
martinbl
Participant
1700 Points
355 Posts
Re: Advantages of using Interfaces
May 24, 2006 06:35 PM|LINK
dsmith3d
Member
30 Points
6 Posts
Re: Advantages of using Interfaces
May 24, 2006 06:47 PM|LINK
As such, the above example would be impossible to do without the interface, since you would have to type cast each object to its derived class:
for (int i = 0; i < obj.Count; i++)
{
if(o is Car) {
((Car)obj[i]).Start();
((Car)obj[i]).MoveBackward();
} else if (o is Bike) {
((Bike)obj[i]).Start();
((Bike)obj[i]).MoveBackward();
}
}
As you can imagine, that would become unmaintainable -very fast- if you planned on implementing more than a few types. So essentially, the interface lets you programatically know that the object contains certain methods, so that you can use those methods without having to know what the derived class actually is.
javiguillen
Contributor
2432 Points
526 Posts
Re: Advantages of using Interfaces
May 24, 2006 07:13 PM|LINK
dsmith3d
Member
30 Points
6 Posts
Re: Advantages of using Interfaces
May 24, 2006 07:20 PM|LINK
-Knowing for certain that specific methods and/or properties are available
-Ablility to call those methods/properties without knowing anything about the derived class
It doesnt seem like much, but when developing systems that require highly reusable/modular code, or when dealing with abstractions where information about the derived classes is simply not available (such as remoting), interfaces are essential.
-Dan Smith
Caddre
All-Star
26581 Points
5308 Posts
Re: Advantages of using Interfaces
May 24, 2006 08:04 PM|LINK
The most important is multiple inheritance, you can get only single inheritance from classes but an interface gives you multiple inheritance.
Gift Peddie
javiguillen
Contributor
2432 Points
526 Posts
Re: Advantages of using Interfaces
May 24, 2006 08:46 PM|LINK
martinbl
Participant
1700 Points
355 Posts
Re: Advantages of using Interfaces
May 24, 2006 10:04 PM|LINK
let's say you still have a IDrivable interface for anything that can be drove
but you also have an IFlyable for anything that can fly..
so if you have a class plane, you will have
public class Plane : IDrivable, IFlyable
{
// implement methods of IDrivable and IFlyable
}