-> First I am not clear with the thing that I am calling the interface method or the class method.
You are calling the class method. You cannot call the interface method. In fact, the interface does not have the method, but just a prototype of it (that is, the definition only).
To be precise, you're calling neither. You are calling the method of the obj
instance of the Program class. To call the method of the class directly like Program.myfunc(10, 20)
, you will need to make the method static.
-> Why do I need Interface then.
You're code here is very simple, so you cannot see the benefit of the interface. Classes seldom match their interfaces like you did. Classes usually have more functionality than the interface. So the interface kind of defines the minimum functionality that the class must have (i.e. implement).
To use simple wording (although not perfectly correct), you use the interface to define a common (that is, shared) functionality among several classes/objects. This makes dealing with those objects much easier without the need to know which object you're dealing with.
For example, le's say you're creating a game with several weapons. You can create IWeapon
interface that has the Speed
and Strength
members as well as the Vibrate
method. Each of your weapons classes will have many more members and methods, but they all must have the Speed
and Strength
members as well as the Vibrate
method. So if you have some code that deals with some weapons (that is, instances of the weapons classes) that one of the characters has and you only need to access the common functionality (defined by the interface) of those weapons, you don't need to know what type of weapon you're dealing with. Let's say the character entered a room with some heavy gas that reduces the speed and strength of all weapons by 10% and causes them to vibrate a little, you can right some code like this:
foreach(IWeapon w in myCharacter.Weapons) {
w.Speed = w.Speed * 0.9;
w.Strength = w.Strength * 0.9;
w.Vibrate();
}
-> When I will have need to create instance of interface like this?
-> Iinterface objinterface=new Program ();
This kind of code is useful when all you need is to access the common functionality defined by the interface and you don't need to access the extra functionality of the object defined by the class. This is especially useful when you are building generic functions that operate on all objects of classes that implement the interface. For example, let's say you want to put the code above in a function that is a method of the gas room:
public class GasRoom {
public void ApplyGasEffect(IList<IWeapon> weapons) {
foreach(IWeapon w in weapons) {
w.Speed = w.Speed * 0.9;
w.Strength = w.Strength * 0.9;
w.Vibrate();
}
}
}
Notice the parameter type IList<IWeapon>
. Now you can pass a list of all weapons that my character has to this function like this:
myGasRoom.ApplyGasEffect(myCharacter.Weapons)
And let's say you want to initially give the player two weapons when they start the stage, and then the player enters the gas room, now your line of code comes in handy:
//Give the player two weapons to start with.
myCharacter.Weapons = new List<IWeapons>();
IWeapon b = new Bazooka();
myCharacter.Weapons.Add(w);
IWeapon r = new Rocket();
myCharacter.Weapons.Add(r);
//Or you can do it all in one line like this:
//myCharacter.Weapons = new List<IWeapons>() { new Bazooka(), new Rocket() };
//////// Here goes some other code for the game //////
//////// until the player enters the gas room ////////
myGasRoom.ApplyGasEffect(myCharacter.Weapons)