Interface as a type in Java?

Let’s declare two interfaces and a class that implements them both: interface I1 { } interface I2 { } class C implements I1, I2 { } objects can have multiple types In the following code, it can be seen that a C instance has the type of C as well as I1 and I2: C … Read more

Why are interfaces preferred to abstract classes?

That interview question reflects a certain belief of the person asking the question. I believe that the person is wrong, and therefore you can go one of two directions. Give them the answer they want. Respectfully disagree. The answer that they want, well, the other posters have highlighted those incredibly well. Multiple interface inheritance, the … Read more

Pros & cons of a callback (std::function/std::bind) vs an interface (abstract class)

I strongly prefer the first way for several reasons: Representing concepts/functionality via interfaces/class hierarchies makes the code base less generic, flexible, and then more difficult to mantain or scale in the future. That kind of design imposes a set of requirements on the type (the type implementing the required functionality) which makes it difficult to … Read more

What is a static interface in java?

I’m curious about the case when it’s not an inner interface. The static modifier is only allowed on a nested classes or interfaces. In your example Entry is nested inside the Map interface. For interfaces, the static modifier is actually optional. The distinction makes no sense for interfaces since they contain no code that could … Read more

Adding a setter to a derived interface

This isn’t a problem: public interface IReadOnly { Data Value { get; } } internal interface IWritable : IReadOnly { new Data Value { get; set; } } internal class Impl : IWritable { public Data Value { get; set; } } The Impl.Value property implementation takes care of both IReadOnly.Value and IWritable.Value, as demonstrated … Read more