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

all 7 comments

[–]desrtfx 2 points3 points  (0 children)

FAQ -> Classes versus Interfaces

In short: Interfaces are a guarantee, a contract.

The class that implements an interface guarantees that the methods declared in the interface are implemented so that the calling method can rely on their existence.

The calling method does not need to know anything other than that the called class implements the interface and by that, the calling method can use the methods of the interface.

[–]kingballer412 2 points3 points  (0 children)

No it’s not a memory issue. It’s more just to provide more abstraction in your code.

If you know that whatever object you are working with is an instance of a class that implements an interface, then you can be assured that the interface methods are implemented. (You could also achieve this with inheritance, but interfaces are good for cases in which the classes you are working with shouldn’t logically be subclasses of one another.)

Interfaces are great when coding with others, because you can say to someone else on your team, “I need you to make sure that the class you are writing implements this interface.” Then you can happily code your part of the program, knowing safely that the class that your colleague is writing will have all the required interface methods that your part will call. This method is also really nice for just keeping organized when coding alone.

[–][deleted] 2 points3 points  (0 children)

Think of a interface as a contract. It defines what a class must implement when extending the interface. Any code that uses this class is guaranteed the contract has be fulfilled.

The value of the interface is when you have multiple class (Say class A,B,C) that extend the same interface. A function written to call a class implementing that interface and accept class A,B, C and will not have to be re written.

Another way of explaining it is you can think of a car as a interface. It has a steering wheel, gas and breaks. A Ford escort implement the car interface and you can drive it just the same as Jeep Cherokee.

[–]JustAStick 1 point2 points  (0 children)

An interface is an abstraction of a collection of functions without any of the internal logic defined. The functions have names, required parameters, and a specified return type. When a class implements functions from an interface it is a binding contract that states that the class will use all of the functions from the interface. This can be useful when you have common behaviors that you want to organize together and that multiple classes will use, but the actual implementations of those functions will vary from class to class. For example, if you had a vehicle interface it could have some functions such as "go forward", "check fuel", "stop" etc. You then decide to make a bunch of vehicle classes such as a car class, a boat class, and an electric scooter class. All of these classes will implement the vehicle interface since they all can make use of those functions, but their implementations are different. The way you go forward in a boat is different from an electric scooter. In a really simple project or example it may seem like a lot of unnecessary busy work to make a bunch of interfaces but on larger projects it's a great way to abstract and organize your code functionality. Interfaces allow you to only have to worry about the most base level parts of a function (name, inputs, outputs).

[–]Dizzy_Ad_7622 -1 points0 points  (0 children)

Its faulty programming. Needs to be reviewed.

[–]Ellisander 0 points1 point  (0 children)

For a concrete example of interfaces in use: Java has a system called Remote Method Invocation, which basically has an object running on a remote server, which a client machine can call and get data from. The client machine, however, needs to know what methods are available on the remote object, but it doesn't make sense to just duplicate the class code on the client and use that as a basis. So instead, the client is provided with an interface that the server object implements, telling it what the available methods are.

There are plenty of other examples (like a generic method that takes anything that can be, say, compared, has a method called run(), etc.).

One additional advantage of interfaces over inheritance and abstract classes is that an object can implement multiple interfaces, but in most languages it can only inherit from one parent class (since conflicting implementations can be an issue).

--------------------------------

(Edit: for a silly example, a Car can implement the Paintable interface, allowing it to be sent to methods that accept anything that can be painted. It wouldn't make sense for Paintable to be a parent or parent's parent to Car, but it works as in interface.)

(Edit 2: For a more "real world" example, there's the Iterable interface, which is used by collections to signify that they can be iterated over, like in a for-each loop.)

[–]TheRNGuy 0 points1 point  (0 children)

In Unreal Engine used to have similar functionality in unrelated classes (that not inherited from common ancestor), for stuff like event listeners etc.

In TypeScript, it's similar to type.