Cast object to interface in TypeScript

There’s no casting in javascript, so you cannot throw if “casting fails”.Typescript supports casting but that’s only for compilation time, and you can do it like this: You can check at runtime if the value is valid and if not throw an error, i.e.: Edit As @huyz pointed out, there’s no need for the type assertion because isToDoDto is … Read more

What is java interface equivalent in Ruby?

Ruby has Interfaces just like any other language. Note that you have to be careful not to conflate the concept of the Interface, which is an abstract specification of the responsibilities, guarantees and protocols of a unit with the concept of the interface which is a keyword in the Java, C# and VB.NET programming languages. In Ruby, we use the … Read more

What is the purpose of the default keyword in Java?

It’s a new feature in Java 8 which allows an interface to provide an implementation. Described in Java 8 JLS-13.5.6. Interface Method Declarations which reads (in part) Adding a default method, or changing a method from abstract to default, does not break compatibility with pre-existing binaries, but may cause an IncompatibleClassChangeError if a pre-existing binary attempts to invoke the method. This error occurs if the qualifying … Read more

What’s the difference between HashSet and Set?

A Set represents a generic “set of values”. A TreeSet is a set where the elements are sorted (and thus ordered), a HashSet is a set where the elements are not sorted or ordered. A HashSet is typically a lot faster than a TreeSet. A TreeSet is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree – I’ve not validated the actual implementation of sun/oracle’s TreeSet), whereas a HashSet uses Object.hashCode() to create an index in … Read more

Fields in interfaces

All fields in interface are public static final, i.e. they are constants. It is generally recommended to avoid such interfaces, but sometimes you can find an interface that has no methods and is used only to contain list of constant values.

Can a normal Class implement multiple interfaces?

A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface. The parent interfaces are declared in a comma-separated list, after the implements keyword. In conclusion, yes, it is possible to do:

How can I implement static methods on an interface?

You can’t define static members on an interface in C#. An interface is a contract for instances. I would recommend creating the interface as you are currently, but without the static keyword. Then create a class StaticIInterface that implements the interface and calls the static C++ methods. To do unit testing, create another class FakeIInterface, that also implements the … Read more