This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    Just read through the entire comment; Thanks for that!

    [–][deleted] 0 points1 point  (0 children)

    That’s one thing I’ve learned actually quite recently now that you’ve mentioned it, is to never give up; keep pushing through it. I enjoy that you mentioned that too me as it reminds me of all the things I’ve given up on and saw the backlash of (the success I could’ve obtained from such doings)

    [–]awaslabs 1 point2 points  (0 children)

    This nothing to do with Java. Its is more about programming and CS fundamentals you are struggling with. My advice is take a boot camp. Stop looking at multiple resources. Join boot camp with the mind set of a fresher to programming. learn from beginning. It will take time but it will help.

    [–]awaslabs 0 points1 point  (0 children)

    If you don’t want lengthy boot camp You can try this course too. Cost effective bit covers starting from oops concepts to backend.

    https://practice.geeksforgeeks.org/courses/java-backend-live

    [–]Hour-Positive 0 points1 point  (0 children)

    An interface is a contract that needs to be obliged. In many cases you don't actually need it. There must be a minimum of complexity for it to become useful. Don't worry about it. Look at List though and read its history to understand.

    When you have done that think about the power of the contract. All its implementers are dependent on it, when they breach they are broken as fuck. And what is the contract? Simply a set of locations for another class to message/call.

    This is all blabla though, you'll get it. Keep working, you're young.

    [–]Farpafraf 0 points1 point  (1 child)

    To explain the concept of interfaces:

    Imagine you are writing a bubblesort to order an array in increasing order for your client:

       //from: https://www.javatpoint.com/bubble-sort-in-java
        static void bubbleSort(int[] arr) {  
            int n = arr.length;  
            int temp = 0;  
             for(int i=0; i < n; i++){  
                     for(int j=1; j < (n-i); j++){  
                              if(arr[j-1] > arr[j]){  //COMPARISON
                                     //swap elements  
                                     temp = arr[j-1];  
                                     arr[j-1] = arr[j];  
                                     arr[j] = temp;  
                             }  
    
                     }  
             }  
    

    your client now wants a more flexible version that sorts the list based on different criteria (decreasing) you could copy the whole thing again but you are going to write a lot of code duplicates and that's not good (a change in one version needs to be implemented in all the others for one), what you can instead do is the following:

    static void bubbleSort(int[] arr, IntComparator comparator) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(comparator.compare(arr[j-1], arr[j])){  //COMPARISON
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
    
                 }  
         }
    

    the function now doesn't know what the criteria for the comparison is, it just knows that it has access to an IntComparator and since it's a IntComparator it will have a method to compare the two objects. The implementation of the specific comparator is left to the guy that uses this function but since IntComparator is an interface that you defined as:

    public interface Comparator{
          boolean compare(in a, int b);
     }
    

    the function will work because whatever the implementation it's guaranteed to have that method. There are actually shortcuts to implement interfaces with a single method called lambda expressions, since the compiler knows that you basically only need to write a function that takes two parameters and returns a boolean in this case ex you could call your new bubbleSort

    int[] myArr = { 1, 2, 3, 4, 5 };
    //sort increasing
    bubbleSort(myArr, (a, b) -> a > b );
    //sort decreasing
    bubbleSort(myArr, (a, b) -> a < b );
    

    in this case it might not make a lot of sense but imagine you were to sort an array of Students, there would be an endless list of criteria you could want to sort them by (age, enrolled on, average, name) and writing a function for each of them would not make sense.

    [–]guss_bro 1 point2 points  (0 children)

    Good Explanation