0

According to Dependency Inversion Principle, a class should not interact with another directly, it should be done using abstraction. That being said, objects are made of type abstract class or interface and are referred to class implementing it. Now my question is if that is the case how can we use additional methods defined in child class? And if not, what is the point of having capability of defining additional methods in child class?

My question is if we try to follow above principle, then we can never use those additional methods. Or there is something that i am missing?

Please reply if this question is not understandable, i will add an example.

  • 1
    This isn't really an accurate summary of what dependency inversion is - see [this answer](https://stackoverflow.com/a/5181887/12299000). – kaya3 Apr 23 '21 at 23:32
  • 1
    A child class can implement additional interfaces. A bat is a mammal but it also has wings and can fly. – Ian Mercer Apr 23 '21 at 23:32
  • Even if it is not accurate definition of DIP, it is achieved using abstraction, if I am correct. And I do understand a class can implement additional interfaces but it will also not help has we will make object of one of those interfaces and not of class. For eg., if C implements A & B, according to DIP, we should make objects of type either A or B. So either we can make C an abstract class and create another class D which implements C and then create object of type C. But even then, my question remains how can we use the additional methods of D which are not present in A, B or C. – Harsh Akshit Apr 24 '21 at 08:54
  • Like, suppose A (interface) has 5 methods that we override in class B and we also add some additional 2 methods. Since making an object of class B in a higher business level module is a bad practice, instead we should make object of concept class i.e. A, then how can we ever use those two additional methods of B. I just want to know if it is possible or not. – Harsh Akshit Apr 24 '21 at 08:58

0 Answers0