-3

Mad, Crazy etc... Question but...

Reference: https://en.wikipedia.org/wiki/Design_by_contract

So the integration of interfaces in object-oriented programming implements the ability to check if a method/property is contained inside an object... but if you're creating the Instance of the object, surely you'd know if it had a certain method?

Reference: https://msdn.microsoft.com/en-us/library/87d83y5b.aspx

Makes no sense why you'd ever need an interface because not even a user input would control any methods, properties or instances.

Reference: https://stackoverflow.com/a/26437718/5897602

Could someone explain why you'd ever need to actually need to need to implement a interface on a class?

Thanks in advance - trying to get my head around them.

Community
  • 1
  • 1
Jaquarh
  • 5,698
  • 4
  • 19
  • 49
  • "So the integration of Interfaces in Object-Orientated Programming implements the ability to check if a method/property is contained inside an Object" - no, it doesn't. It allows multiple different implementations to be used by code which only needs to know about the interface. I don't know where you got the "checking" part. – Jon Skeet Mar 11 '16 at 16:40
  • 4
    Volumes have been written on this. – Big Daddy Mar 11 '16 at 16:40
  • That is my issue, I am really not understanding where you'd need to use a Interface @JonSkeet & If you could mark duplicate, I'd be happy to look because I have searched and still cannot get my head around the particular reason you'd use a Interface – Jaquarh Mar 11 '16 at 16:45
  • 3
    Imagine we didn't have interfaces (or inheritance in general) - you couldn't write code that could write to a network stream *or* a file stream *or* an in-memory stream. You couldn't write a sort method that would sort *any* mutable collection... – Jon Skeet Mar 11 '16 at 16:46
  • It's crazy how adding a signature to an Object can actually optimise so much, thanks a lot for this @JonSkeet – Jaquarh Mar 11 '16 at 16:51

2 Answers2

2

Interfaces allow the implementor to say Hey, I can do this, rather than I am this.

A real life example:

You are a human being (class Human), you can bring coffee (interface ICanBringCoffee). I wouldn't say you are a coffee bringer, it tells you your ability to do so. You can teach your ape to do the same, but he isn't a human. He can 'implement' ICanBringCoffee though, where his way to do this (called 'implementation') is different than yours.

With an interface you define a contract that allows to tell what you can deliver to the other party:

ICanBringCoffee c = someHumanOrApe;

c.BringCoffee();

Here you can see the benefit: I don't need to know you are Human and bring coffee. I can simply rely on our agreement you can do this, and apes too.

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
  • So integrating this to a bit of software actually helps the back-end for a business approach to it? Because although, like you say, most things can be trained to bring coffee, you're just adding that extra information to really isolate what is happening? – Jaquarh Mar 11 '16 at 16:48
  • Yes, sometimes you don't know the specifics, or don't want to. You just rely on the agreement made: the contract or interface. – Patrick Hofman Mar 11 '16 at 16:49
  • So they're not necessary in all Objects but can optimise the understanding if used? Thanks a lot for that answer, that really does make sense & I'm going to share this around because I know many people who - like me - got confused with this idea. – Jaquarh Mar 11 '16 at 16:50
0

Put simply interfaces are used as class signatures. They show you what the class contains but hide the concrete implementations of methods. One use would be for a public web service, you want to let the world know what methods can be used but do you want to show the world how exact calculations are made in the methods?

http://www.cs.utah.edu/~germain/PPS/Topics/interfaces.html

James Dev
  • 2,879
  • 1
  • 9
  • 16