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

Show parent comments

2

u/SavvyPlatinum Apr 06 '22 edited Apr 06 '22

Edit: I've just learnt that sub bullet points do not work well on mobile. I'd recommend viewing on web.

Sure! Here's a more detailed explanation.

Firstly, there are key differences between the two you need to be aware of that might change why you want to use each of them.

  • A class can only inherit one abstract class, but it can implement multiple interfaces.

  • Methods in abstract classes can be private, public or protected, but in interfaces it can only be public.

  • Variables in abstract classes can be static or non-static, final or non-final, but in interfaces it can only be static and final.

  • Methods can have implementations in abstract class but they must be empty in interfaces.

The main purposes of these two things are different, which you might have noticed from the key differences.

  • Abstract Classes are meant to help with inheritance, where you want to create objects that are a sub-type of another object.

    • If you want to create many types of objects that are only slightly different from each other, you'd want to use abstract classes.

    • Let's say you want to create 2 types of watches: smart watches and automatic watches. Implementations like "check time" and "tighten strap" are the same in the two watches. Therefore you add the method implementations into the abstract class Watch, and leave others like "wind watch (one you shake your wrist, one you replace battery)", "click screen (one doesn't even have this)" etc to the sub-classes to implement. Both types of watches also have straps which are exactly the same, so you just add a Strap variable to the abstract class and you don't have to add them to the subclasses.

    • Because of the way that you're saying "the class is a sub-type of the abstract class", you can only inherit one abstract class per class.

  • Interfaces are meant to tell people exactly what an object should do. It's a contract that all objects agree to that they will provide certain functionalities.

    • If you want to say that certain objects must be able to something, you'd want to use interfaces.

    • Let's say you want to define what can be done in things with a battery. Things with a battery can be "turned on", "turned off" and "replace battery". Many different things can do these but in a multitude of different ways, so you just say the interface IBatteryOperable have those 3 methods.

    • Because of the way that you're saying "the class must be able to do the following things", you can only have public methods in them, but a class can implement multiple interfaces.

  • Using the examples above, we can do the following things.

    • A smart watch can inherit Watch and also implement IBatteryOperable.

    • An automatic watch would only inherit Watch.

    • The Watch class might implement something like IStrappable which has methods like "strapOnWrist" and "replaceStrap" (Of course you might instead want to have a Strap object in Watch that does those things but that's a separate topic).

    • Something like a torch would also implement IBatteryOperable.

    • If I want to turn on everything I have, I can make List<IBatteryOperable> and for each item in the list I do TurnOn(). The computer would then use "turnOn()" in both Torch and SmartWatch, using the implementations in each of the classes.

    • If I want to sync the time of all of my watches, I can make a List<Watch> and for each item in the list, I can check the "timeDisplayed" variable then "setTime" to correct them if the time is wrong.

Ultimately it's up to you how you want to use these classes and what you need to use them for. But the general convention is you use interface to "define what it can do" and abstract class to "define what type of thing it is".

Now to answer your questions.

In C# 8, interfaces can have default implementations, so for me, its the same as an abstract class?

I haven't used C# in quite a while so I wasn't aware of this. I Googled it a bit just now, and here's what it looks like it does (someone please correct me if I'm wrong). The default implementation in interfaces forces all subclasses to use that implementation. In abstract classes, the subclasses can override the default implementation simply by creating its own implementation. But here, if you try to create your own implementation in the subclass it will not compile. This is basically used where you know FOR SURE that no subclasses will implement this method differently. It looks like the main difference is that the methods have to be static in the interface since interface cannot have instantiated variables. The main advantage is that if you add a default method in the interface, it won't break the contract in the subclasses so you won't have to add the methods there too.

Is the main difference that an interface forces you to implement all its methods, where you dont have to use all from an abstract class?

In the case of abstract class, the only way you can not implement a method in a subclass is if the the method is already implemented in abstract class. If you only write the method signature in the abstract class without implementing it, you still have to implement it in the subclass.

Also quick note here that if there's an implementation of the same method in both the abstract class and the subclass, the subclass version will always be used.

Why an abstract class over a normal parent class?

Because abstract classes cannot be instantiated. In the example above, you don't want to make Watch a normal class, because without knowing what specific type of watch it is, you won't be able to wind the watch. You want to use normal parent class only if it's already a complete object by itself, but the subclass only extend certain features or functionalities.

Do you have an example of where to use them in a real life scenario?

Why an interface instead of an abstract class?

(See detailed explanation above)

Hope this helps!

2

u/Brafaan Apr 06 '22

THANK YOU! For the first time I actually understand! Thank you very much!!!😁😁

1

u/SavvyPlatinum Apr 06 '22

No problem! Happy to help!

2

u/CodeMonkeeh Apr 06 '22

I Googled it a bit just now, and here's what it looks like it does (someone please correct me if I'm wrong). The default implementation in interfaces forces all subclasses to use that implementation. In abstract classes, the subclasses can override the default implementation simply by creating its own implementation. But here, if you try to create your own implementation in the subclass it will not compile.

If I'm understanding you right, then no.
Default implementations in interfaces can absolutely be overridden by implementing types.

https://dotnetfiddle.net/KBbQfy

2

u/SavvyPlatinum Apr 06 '22

Huh, interesting thanks for checking. I guess the main difference then is that because interfaces can't have instantiated variables, the methods will essentially have to be static methods. But yeah the method works the same as if it was in a class.

I hate it lol.

u/Brafaan please disregard my answer about the C# interface. Methods can indeed be overridden even with an interface.

1

u/CodeMonkeeh Apr 07 '22

Static interface members is a separate thing.

You can have an instance member with a default implementation.

IIRC they had two reasons for this feature:

  1. It allows you to add members to an interface without breaking every implementing type. This is especially nice for library authors.

  2. It basically acts as the traits concept.