Is it possible to specify a static function in a Kotlin interface?

Basically, nothing in a companion object can be abstract or open (and thus be overridden), and there’s no way to require the implementations’ companion objects to have a method or to define/require a constructor in an interface. A possible solution for you is to separate these two functions into two interfaces: interface Serializable<ToType> { fun … Read more

Why duck typing is allowed for classes in TypeScript

This is the way structural typing works. Typescript has a structural type system to best emulate how Javscript works. Since Javascript uses duck typing, any object that defines the contract can be used in any function. Typescript just tries to validate duck typing at compile time instead of at runtime. Your problem will however only … Read more

The difference between liskov substitution principle and interface segregation principle

LSP: The subtype must honor the contracts it promises. ISP: The caller shouldn’t depend on more of the base type’s interface than it needs. Where they fit: If you apply the ISP, you use only a slice of the receiver’s full interface. But according to LSP, the receiver must still honor that slice. If you … Read more

Persist collection of interface using Hibernate

JPA annotations are not supported on interfaces. From Java Persistence with Hibernate (p.210): Note that the JPA specification doesn’t support any mapping annotation on an interface! This will be resolved in a future version of the specification; when you read this book, it will probably be possible with Hibernate Annotations. A possible solution would be … Read more

Creating an interface for an abstract class template in C++

You are actually attempting the impossible. The very heart of the matter is simple: virtual and template do not mix well. template is about compile-time code generation. You can think of it as some kind of type-aware macros + a few sprinkled tricks for meta programming. virtual is about runtime decision, and this require some … Read more

How to define the default implementation of an interface in c#?

Here comes the black magic: class Program { static void Main() { IFoo foo = new IFoo(“black magic”); foo.Bar(); } } [ComImport] [Guid(“C8AEBD72-8CAF-43B0-8507-FAB55C937E8A”)] [CoClass(typeof(FooImpl))] public interface IFoo { void Bar(); } public class FooImpl : IFoo { private readonly string _text; public FooImpl(string text) { _text = text; } public void Bar() { Console.WriteLine(_text); } … Read more

In java 8, why cannot call the interface static method that the current class is implementing [duplicate]

Addition of static methods in interface in Java 8 came with 1 restriction – those methods cannot be inherited by the class implementing it. And that makes sense, as a class can implement multiple interface. And if 2 interfaces have same static method, they both would be inherited, and compiler wouldn’t know which one to … Read more