locked
i just read something new about Interfaces RRS feed

  • Question

  • User1457412228 posted

    and I wanted some advice/thoughts on it from others.

    I see one can use an interface to indicate a method variable type, such as

    public string test(ICompariable v)

    { ...

    And of course a custom interface could be used.

    Anyway, this seems to open up about half again all the possibilties on how one might do things in an object oriented world.  Any thoughts on actually using this approach?

    Thanks

    Thursday, January 22, 2009 1:52 PM

Answers

  • User1135837648 posted

    Right... whatever "noun" (Object) you receive, if you accept a certain set of behavior (Interface), you then know for a fact that your "noun" (Object) has the set of behavior your accepting (Interface).

    In fact, most of the objects already have an interface... every method declared public in an object is automatically part of its "implicit" interface.

    I suggest you read about polymorphism to get all the details.

    Hope that helps,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2009 5:36 PM
  • User-1179452826 posted

    Look at these two methods:

    public bool test(int i)

    {

    ...

    and

    public bool test(IComparable i)

    {

    ...

     

    The first is constrained to work only with an integer while the latter can work with any object that implements IComparable. Another approach to achieve this flexibility would be to use generics, but even then, a separate method would be generated by the compiler for each type of parameter passed in.

    Now say you wish to work on all types of classes that have a common behaviour. Would you fix your method to work with only one class, or all classes that support the behaviour?

    Interfaces are vital in good code design and helps elevate code reuse. It's a fundamental part of OOP.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2009 11:32 PM

All replies

  • User1135837648 posted

    Hello,

    isn't it just some good old polymorphism?

    What do you mean exactly?

    Best regards,

     

    Thursday, January 22, 2009 2:47 PM
  • User1457412228 posted

    i understand we are to think of Objects as nouns and interfaces as a collection of behaviors.  passing in a noun is much different than passing in a set of behaviors. 

    Thursday, January 22, 2009 4:42 PM
  • User1135837648 posted

    Right... whatever "noun" (Object) you receive, if you accept a certain set of behavior (Interface), you then know for a fact that your "noun" (Object) has the set of behavior your accepting (Interface).

    In fact, most of the objects already have an interface... every method declared public in an object is automatically part of its "implicit" interface.

    I suggest you read about polymorphism to get all the details.

    Hope that helps,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2009 5:36 PM
  • User-1179452826 posted

    Look at these two methods:

    public bool test(int i)

    {

    ...

    and

    public bool test(IComparable i)

    {

    ...

     

    The first is constrained to work only with an integer while the latter can work with any object that implements IComparable. Another approach to achieve this flexibility would be to use generics, but even then, a separate method would be generated by the compiler for each type of parameter passed in.

    Now say you wish to work on all types of classes that have a common behaviour. Would you fix your method to work with only one class, or all classes that support the behaviour?

    Interfaces are vital in good code design and helps elevate code reuse. It's a fundamental part of OOP.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2009 11:32 PM