-1

I've seen some examples of Interfaces and Abstract Classes and how they can be connected to each other. I've been wondering how important are these two in object-oriented programming.

Why is it that some methods have to pass objects instantiating Interfaces and Abstract Classes first before adding it to a normal class when you can directly add it to a normal class? Is it for the security of your code?

Katie
  • 2,594
  • 3
  • 23
  • 31
JTrixx16
  • 1,083
  • 9
  • 15
  • 1
    Abstract classes and interfaces provide a contract for your code saying they will have certain methods, without implementing them. There only important if you plan to extend the code with multiple classes, because code that uses the classes can count on the methods existing. Abstract classes can implement functionality, interfaces cannot. You can only inherit from one class at a time, but you can implement several interfaces in the same class. – ArtisticPhoenix Oct 09 '16 at 07:11

1 Answers1

3

Defining both interfaces and abstract classes are design mechanisms that help create solid software architectures. It doesn't have to do with security. They provide the ability create classes that best capture the real-world situation in which the software is trying to achieve.

Why is it that some methods have to pass objects instantiating Interfaces and Abstract Classes first before adding it to a normal class when you can directly add it to a normal class? Is it for the security of your code?

These are all architectural choices by the developer of the code and related to Dependency Injection, you can also read a good SO document about Abstract Classes, that covers both Abstract classes and Interfaces and how they interact.

miken32
  • 42,008
  • 16
  • 111
  • 154
Katie
  • 2,594
  • 3
  • 23
  • 31