Why to use Interface.

I just want to know, why we need to use interface.Can't we do writ

Page 1 of 1

4 Replies - 926 Views - Last Post: 22 November 2009 - 10:01 AM Rate Topic: -----

#1 rock_kane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-November 09

Why to use Interface.

Post icon  Posted 21 November 2009 - 11:33 AM

I just want to know, why we need to use interface.Can't we do the same thing by writting the functions/properties directly in the class, instead of using interfaces.
Is This A Good Question/Topic? 0
  • +

Replies To: Why to use Interface.

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Why to use Interface.

Posted 21 November 2009 - 11:39 AM

I'm guessing you can write the functions and stuff but it would take a lot of time of your hands.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Why to use Interface.

Posted 21 November 2009 - 11:50 AM

An interface doesn't provide the methods, what it does is provide a contract that those methods will exist. This allows you to use code that just knows that the interface has been implimented.

The standard example goes something like this:
nterface IAnimal {
	public void eat();
}

class Dog : IAnimal {
	public void eat() {
		Console.WriteLine("Chomp, Chomp");
	}
}

class Cat : IAnimal {
	public void eat() {
		Console.WriteLine("Nom, Nom");
	}
}

class Program {
	static void Main(string[] args) {
		List<IAnimal> animals = new List<IAnimal>();
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			if (r.Next(2) == 0) {
				animals.Add(new Dog());
			} else {
				animals.Add(new Cat());
			}
		}

		foreach (IAnimal whatsThis in animals) {
			whatsThis.eat();
		}
	}
}


The IAnimals interface 'forces' us to implement an eat() method. Then, in the main code we can refer to either a dog or a cat as an IAnimal object, since we don't care which it is, and call the methods that we know have to be there because of the IAnimal interface.

This is useful when you aren't sure what kind of data you are going to be working with, but you want to perform actions on that object. Another example is a Sort method. If you know that anything to be sorted implements the IComparable interface, you can use the CompareTo() method and not worry about the objects themselves, just your sort method. This decreases coupling (and as we all know, coupling is bad! :)) This leads to code reuse, as you only have to write your Sort method one time, and it can sort anything that implements IComparable.

This post has been edited by Momerath: 21 November 2009 - 11:52 AM

Was This Post Helpful? 1
  • +
  • -

#4 rock_kane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-November 09

Re: Why to use Interface.

Posted 22 November 2009 - 03:09 AM

View PostMomerath, on 21 Nov, 2009 - 10:50 AM, said:

An interface doesn't provide the methods, what it does is provide a contract that those methods will exist. This allows you to use code that just knows that the interface has been implimented.

The standard example goes something like this:
nterface IAnimal {
	public void eat();
}

class Dog : IAnimal {
	public void eat() {
		Console.WriteLine("Chomp, Chomp");
	}
}

class Cat : IAnimal {
	public void eat() {
		Console.WriteLine("Nom, Nom");
	}
}

class Program {
	static void Main(string[] args) {
		List<IAnimal> animals = new List<IAnimal>();
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			if (r.Next(2) == 0) {
				animals.Add(new Dog());
			} else {
				animals.Add(new Cat());
			}
		}

		foreach (IAnimal whatsThis in animals) {
			whatsThis.eat();
		}
	}
}


The IAnimals interface 'forces' us to implement an eat() method. Then, in the main code we can refer to either a dog or a cat as an IAnimal object, since we don't care which it is, and call the methods that we know have to be there because of the IAnimal interface.

This is useful when you aren't sure what kind of data you are going to be working with, but you want to perform actions on that object. Another example is a Sort method. If you know that anything to be sorted implements the IComparable interface, you can use the CompareTo() method and not worry about the objects themselves, just your sort method. This decreases coupling (and as we all know, coupling is bad! :)) This leads to code reuse, as you only have to write your Sort method one time, and it can sort anything that implements IComparable.

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Why to use Interface.

Posted 22 November 2009 - 10:01 AM

Was there a question in there, or did you just quote me because of my awesomeness?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1