Can someone breakdown the use of Interfaces asif explaining it to a 5 year old?

This is for lesson 29 of the new RPG course. I don’t consider myself a beginner anymore, but the whole interface thing went way over my head. Terminology is used in a way that assumes you understand what is being said. For example what does it mean when someone says the interface is a contract? What is meant by contract? I don’t see a place for parties involved to sign and date like you would in a contact. Clearly the penny is still up in the air for me on this one.

Right now I am just blindly following along. Is there another way of explaining what an Interface is, as if teaching a 5 year old, who is hearing about this stuff for the first time? Is Interfaces just for circumventing dependency issues, or is it used for something else as well? If so what and when?

Thanks in advance to everyone who contributes in resolving this for me.

3 Likes

:slight_smile:
1 Like

Hopefully the above video will help but this is intermediate level coding :slight_smile:

Now i am not going to sit here and pretend i know it all because to be honest i am having to do extra reading and advanced courses just to stay one step ahead of you guys!

Now one thing i am gonig to suggest is the course isnt going anywhere.
You have lifetime access to it and so it will be there. And we are going to head straight into part 2 after its done.

What i suggest doing is taking these aspects we are learning here and applying them to other games.

I’ve been doing this recently with my non RPG game

Started out as a ball picking up cubes, Now it has view changes, jumps, murder mode, fading sequences and the like

Its worth taking a game and running with it as it will help prepare you and if specific classes on topics like the above fromt he unity website help go for it :slight_smile:

Thanks Jacob and Marc. I actually have Mosh’s intermediate course, and watched the video series on Interphases. Unfortunately they seem to be more about Testability, Extensibility and Poly morpher…whatever. The focus does not seem to be on Interfaces and the example is rather complex. It would be nice to have it broken down into a way more simple problem. I know I will get it eventually, but the explanation in both Mosh’s course and this one doesn’t quite meet the needs of someone seeing interfaces for the first time. Even though I am no longer a beginner - I am a beginner to Interfaces, but the teaching pitch is not at a beginner level - there has got to be a better way to explain it. I will persevere though. Please let me know if you come across a better explanation (and not Brackeys tutorial as that was absolutely useless and pointless and simply incorrect)

1 Like

Okay :slight_smile: - Let me take a try.

Lets say you have a Zoo in your code with a lot of different animal classes like monkey, tiger, penguin and eagle.

in your code you have a method, that only works if the animal can fly.

How do you do that? - you want the method to be typesafe so it only accept a class that have a property/method with fly.
One way would be to create a inheritance structure - so you have animal as base class and bird/mammel etc. as a 2nd. and you create the property/method in the birdclass.

Now you can change the method to accept a bird class or any class that inherit from bird.

Well…one problem. A penguin is a bird, but can’t fly.

So interfaces is a way to solve that.

You create an interface and state the property/Method fly, and now you change your method to accepts the interface.
This means that all classes that implements the interface have the property/method fly and the method accepts it.

It is now typesafe and can take all animal classes as long as they implement the interface.

That is why the call it a contract - when implemented the class need to meet the requirement in the interface or it won’t compile.

Hope it helps.

4 Likes

Interfaces also have a 2nd use which might come in handy.

Lets say you want to create a class that takes a product and calculate a discount.
Here you can create a member variable of the type interface - and supply it with a class that implements the interface.
The interface will here make sure that the given class can calculate discount.

The next week you need a new discount, so you create a new class and implement the same interface - now the membervariable can be set to the new class, but you didn’t have to change your code in that class.

I know Mosh talks alot about extensibility loose coupling etc. but it is the reasons to use them in the first place :slight_smile:

4 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.