Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • what is the main purpose technically speaking about the abstraction in programming using DI, Interfaces, and Abstract Classes?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.95k
    Answer it

    I viewed a couple of answers online regarding Abstractions, Abstract Classes, Interface's, DI, and Loose coupling. But none of these answers are answering my question. I grouped these topics because they are related to achieving abstractions. Got a good understanding of the mentioned topics, but yet not fully understand them in detail and how they related to each other.

    Generally speaking, interfaces are used to make classes loosely coupled. Thus define a set of functions and fields to be implemented. The idea of making Loosely Coupled classes is that will need to remove dependency over several classes.

    For instance, if we make a change to one of these classes then we do not need to change other places making code maintainable. The only good example I can think of to use loosely coupling is through DI. So when we say interfaces make classes loosely coupled do we mean by passing an interface as a dependency?

    "Please continue reading will further clarify".

    A question here is if we are going to use DI and pass interfaces as dependencies then why not pass a class as a dependency instead? maybe I will need further clarification about Interfaces before answering the previous question. I will further explain.

    The main idea of interfaces is to establish contact with classes that going to implement the interface meaning we are going to need to define functions and fields to enforce to implement them. but still, the idea of interfaces as a contract is not yet clear because if we enforce a developer to implement an interface called a server that has methods to turn on and off the server but the developer forgot to turn off the server programmatically then what is the point of this contract?

    Further, my understanding is that this all falls under the concept of abstraction which means we do not need to worry about details but an abstraction. Does that not mean when building an application we first need to create classes/structures without code such as using UML?

    Further, why would we use an abstract class over interfaces where an abstract class has similarities to an interface such as defining a function but without a body?

    Coming back to Interfaces and DI we can inject interfaces as a dependency but why? Can we not inject a class it self? is it not easier to use classes as a dependency? where we can access all functions or this is not the idea of interface Can sombody help with this. I only understand one use case on why we should use DI. Example:

    //Class1 
    //Class1 Con
    Public Class1 Con(){
     Class2 class2 = new Class2(1,1,1)
    }
    

    The above example is not maintainable because if we add a new parameter to Class2 then we need to modify it elsewhere. but if we use DI Injection then we won't are there any other reasons.

    Also, DI can be useful to create one instance and use that instance across the whole application. Does that save some memory by not creating multiple instances? or saving time connecting to DI?

    The question should we use abstractions at the very early coding stage where we create classes without code?

    Further, do we use interfaces to make the developer aware that they need to implement a certain set of functions? But why?

    Do I predict that we need to use an interface by creating UML diagrams to see if there would be different classes to use an interface with similar functionalities

    "Can we not just create a superclass and override methods"

    Can somebody explain when to use superclass and override methods over interfaces and provide an implementation?

    Also, when to pass an interface as a dependency? And when to pass a class as a dependency? One advantage I can think of when using interfaces is polymorphism where we can make an interface of any implemented types and then access the interface type function; polymorphism. Example:

    Class1 class = new Interface1();
    

    Can this be possible?

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: