r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

3

u/[deleted] Apr 05 '22

Interfaces.

I get that it's used for inheritance, but I cant quite wrap my head around when or why you'd need to write one.

5

u/CatatonicMan Apr 05 '22

Interfaces are effectively a contract that tells you what a thing does (at minimum). They allow old code to use new code.

Say I have an Animal interface that defines a method Eat - I'm making a guarantee that all Animals can Eat, and that calling Eat on an Animal - any Animal - is a valid thing to do. If it can't Eat, it's not an Animal.

If I later create a Cat that inherits from Animal, I know the Cat can Eat, because it's an Animal and Animals can Eat.

The crucial bit is that I can write code that takes an Animal - Feed(Animal), say - and that code will work on anything that inherits from the Animal interface.

I can Feed(Cat) because Cat is an Animal. I don't need to know anything more about the Cat. I can Feed(Dog), because Dog is an Animal, too. Feed(Snake). Feed(Tortoise). Feed(Alpaca).

Years later, someone I've never met can make their own Animal (Lobster) and my code will have no problems working with it.

4

u/door_of_doom Apr 05 '22

An improvement that I'd like to make to this answer: We generally don't call it inheriting from an interface, we generally refer to it as implimenting an interface.

Cat doesn't inherit the Animal interface, it implements the Animal interface.

1

u/__Fred Apr 06 '22 edited Apr 06 '22

Yes. A lot of OOP is for organizing code, so other people, or yourself in the future can understand it.

You don't need to have OOP to achieve anything technical. The processor doesn't understand what an interface is.


Why don't just use classes with methods that don't do anything?

Because it gives people the option to inherit from another class and still implement the interface. Because you can only ever inherit from one class (e.g. in Java).

Also, it prevents people from trying to create objects from your bare "interface-class" that is only meant to be inherited/implemented.

3

u/door_of_doom Apr 05 '22

Think of the Java Interface List. (https://docs.oracle.com/javase/8/docs/api/java/util/List.html)

It talks about a lot of things that can be done to a List. You can add things to a List, remove things from a list, perform actions against each element of a List, etc. There are a LOT of things that can be done to Lists!

But get this: It is impossible to actually create a "List" in Java, because a "List" is just an interface; It's just a contract defining a wide variety of possible lists that one might want to create. In order to actually create a List, what we need to create is something that implements the List interface. The most commonly used List implementation is ArrayList, which is an Array that conforms to the List interface.

So if I make a function called ListPrinter that accepts a List as a parameter and prints out all of its contents, I can declare any valid implementations of List as my parameter, and I can easily accept an ArrayList, LinkedList, Stack, Vector, or any other implementation of the List interface as a parameter of my function, and I'll be able to work with all of them equally well because they all implement the same method names that I need in order to do what I need to do: Print out their contents.

1

u/starkat0w0 Apr 06 '22

An interface is a contract between developers.

Let’s say I make a Pokémon app. I decide that I want to make a “Pokémon” object. I want you to think of what properties and methods you would give a Pokémon. Chances are you would describe it differently than me. I (the person originally writing the code) decide that a Pokémon must have a name, color, type, and region. I build my entire code around this Pokémon object using its constructors in my code.

5 years later you come across my code and decide you want to make a Pokémon. If I don’t have an interface telling you what a Pokémon MUST have in order to work with the code I have written, what is stopping you from making something else? What if you decide a Pokémon will have just a name? It will completely break my code because I am depending on all Pokémon to have a name, color, type, and region.

So to prevent this I make an interface for my Pokémon object. An interface does not assign any values, only declares that certain values should be present in any object that implements the interface.

Example:

Pokémon interface Name: string Color: string Type: string Region: string

Pokémon object Name: pikachu Color: yellow Type: electric Region: idk I don’t know Pokémon that well lol

If I try to make a Pokémon without, for example, a name, my code will yell at me and go NO STOP YOUR POKÉMON MUST HAVE A NAME!

When an interface is used, you can make a new Pokémon that has new properties or methods but it MUST at LEAST have the properties and methods described by the interface.

1

u/lionhart280 Apr 06 '22

Decoupling is the usual term.

Lets say I define an interface, IMyThing, and I say it will have the method DoTheThing

 public interface IMyThing {

      void DoTheThing();
 }

Aight, now I can implement it with a class and the compiler will demand I make sure my class has a function called DoTheThing() on it, it has to because the interface requires it.

Many people refer to this as a "contract".

Now, lets say we have numerous implementations. MyThingA, MyThingB, MyThingC, all of which implement IMyThing

You can do stuff like have an IList<IMyThing> which has a mixture of MyThingA/B/C in it. You can have a method take in a IMyThing as a parameter.

For example you could do:

 public void SomeConsumingFunction(IMyThing myThing) {
      myThing.DoTheThing();
 }

 public void OuterFunction(MyThingA thingA, MyThingB thingB, MyThingC thingC) {
      SomeConsumingFunction(MyThingA);
      SomeConsumingFunction(MyThingB);
      SomeConsumingFunction(MyThingC);
 }

Note how the OuterFuntion knows the types, but that inner consuming function just takes in the generic interface, because it doesn't need more than that.

1

u/Grination Apr 11 '22

One of the advantages of interfaces is that they don't break your code if you decide to switch the implementation some day. For instance, in Java, there is a standardized persistence specification called JPA (java persistence api), and there are many vendor implementations of it such as hibernate and eclipseLink, in your code you don't directly depend on the implementation, you only depend on the interface, so if you decide to switch one day from an implementation to another, you'll only change a .properties configuration file specifying which class to use, without messing about with your java classes

1

u/[deleted] Apr 11 '22

Yeah, trying to build a full stack web app with hibernate is where I found it the most confusing.