I am trying to understand Interfaces and why I would use them.
I have an example from a book that is explaining it.
I just can't seem to see why I would use it.
In my example, it has 2 interfaces: IDisplayable and IComparable
(Predefined).
In the Students class part, he is moving the students array to an
IComparable Object array and then sorting it.
// utilize the IComparable interface to sort the
// array
IComparable[] comparableObjec ts = (IComparable[])students;
Array.Sort(comp arableObjects);
And then it moves the student array into the new IDisplayable interface:
// now the IDisplayable interface to display the results
IDisplayable[] displayableObje cts = (IDisplayable[])students;
DisplayArray(di splayableObject s);
And as is shown in the Birds section, I just do:
Array.Sort(stud ents);
DisplayArray(st udents);
I get the same results.
And if I have to implement all the functions described in the Interface,
what am I really inheriting?
Why not just use an abstract class to do this?
Just a little confused.
Thanks,
Tom
I have an example from a book that is explaining it.
I just can't seem to see why I would use it.
In my example, it has 2 interfaces: IDisplayable and IComparable
(Predefined).
In the Students class part, he is moving the students array to an
IComparable Object array and then sorting it.
// utilize the IComparable interface to sort the
// array
IComparable[] comparableObjec ts = (IComparable[])students;
Array.Sort(comp arableObjects);
And then it moves the student array into the new IDisplayable interface:
// now the IDisplayable interface to display the results
IDisplayable[] displayableObje cts = (IDisplayable[])students;
DisplayArray(di splayableObject s);
And as is shown in the Birds section, I just do:
Array.Sort(stud ents);
DisplayArray(st udents);
I get the same results.
And if I have to implement all the functions described in the Interface,
what am I really inheriting?
Why not just use an abstract class to do this?
Just a little confused.
Thanks,
Tom
Comment