Hacker News new | ask | show | jobs
by homosaphien 3298 days ago
This probably is ignorant, But I don't get why we need interface in java/c#. Why can't a base class with abstract methods be sufficient for that use case?
4 comments

It allows you to compose types and avoid the issues (complications not necessarily problems) of multiple inheritance.

For example, where in your hierarchy do you stick the AbstractSerializable class? If it's an interface like ISerializable you just implement it at whatever level you're at.

With multiple inheritance you don't need interfaces because you can just inherit from multiple base classes.

Sadly, ever since Java 8 added default methods to interfaces this distinction is even murkier. I'll add the reasons why I prefer interfaces though.

1. Interfaces can be used by any class no matter where they live in their class hierarchy. You can only extend a single (abstract) class in Java because the language designers saw the special rules and overrides other OOP languages like C++ used to avoid the 'diamond problem' and decided against it entirely. Note that in pre-Java 8 implementing multiple interfaces cannot lead to the diamond problem because if more than one interface exposes the same method signature then the compiler doesn't care because it doesnt have actual functionality that could be conflicting. So, because you are limited to only extending one superclass it is easier to bundle functionality in multiple smaller interfaces that can be implemented no matter where your implementing class lies in the class hierarchy.

2. Interfaces provide only a contract of functionality which in practice means they are easier to reason about and use. If you are implementing an interface you dont have to worry about calling a super constructor or any other garbage. Just implement the method signature. Of course you could ask "What if you only had abstract methods with no implementation in an abstract class", in which case I would refer you to point 1. A class that already has some existing superclass could not extend that abstract class.

I hope that clears some stuff up. Just in case you read that and thought the Java language designers were stupid for adding default methods in Java8 they had really good reasons to (mostly to support backwards compatibility with a large change called Lambdas). But having some functionality in interfaces could be argued for as well. In practice there are companion classes in the Java API that are just there to provide some base functionality that they could not do because of the previous limitations of interfaces. Also, the Java8 rules for fixing 'the diamond problem' with conflicting default methods in interfaces is pretty straightforward and rarely encountered.

Interfaces were created because you can't extend multiple classes. It's the languages way of allowing polymorphism. You very well can create base classes and extend from them, but you will likely come across a time where you want to inherit from different things and interfaces are the only way to do that.
have almost same issue